I had the same problem, whereby I wanted a test harness to point to a couple of services. Each service would have datacontracts in common.
What needs to be done:
- Use svcutil with /t:metadata on each url.
- Rename all the generated files with something unique for the service (e.g. Rename lala.xsd to 1_lala.xsd)
- Copy all generated files to a single location
- Use svcutil with *.xsd .wsdl /out:output.cs /namespace:,MySpecialNamespace to generate ALL service contracts and datacontracts to a single file.
If you want to be crafty: use the following T4 template:
<#@ template language="C#v4.0" hostspecific="True"#>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.IO" #>
<#=GetGeneratedCode(
"http://localhost/Service/Service1.svc",
"http://localhost/Service/Service2.svc",
"http://localhost/Service/Service3.svc",
"http://localhost/Service/Service4.svc"
)#>
<#+
const string _svcutil = @"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\svcutil.exe";
private string GetGeneratedCode(params string[] urls)
{
var tmp = GetTemporaryDirectory();
foreach (var url in urls)
{
GetMetadata(url, tmp);
}
RunSvcutil(tmp, "*.wsdl *.xsd /out:output.cs /namespace:*," + Path.GetFileNameWithoutExtension(Host.TemplateFile));
var result = File.ReadAllText(Path.Combine(tmp, "output.cs"));
return result;
}
private static void RunSvcutil(string workingFolder, string arguments)
{
var processInfo = new ProcessStartInfo(_svcutil);
processInfo.Arguments = arguments;
processInfo.WorkingDirectory = workingFolder;
var p = Process.Start(processInfo);
p.WaitForExit();
}
private static void GetMetadata(string url, string destination)
{
var workingFolder = GetTemporaryDirectory();
RunSvcutil(workingFolder, string.Format("/t:metadata \"{0}\"", url));
foreach (var filename in Directory.GetFiles(workingFolder))
{
File.Copy(filename, Path.Combine(destination, Path.GetFileNameWithoutExtension(url) + "_" + Path.GetFileName(filename)));
}
}
private static string GetTemporaryDirectory()
{
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);
return tempDirectory;
}
#>