11

VS WCF integration has nice option "Reuse types in refrenced assemblies". The problem is that I need the same but for the current assembly. Some of the types are already defined in my assembly and I need to reuse them.

Usage scenario:

  1. I have assembly, and have TypeA here.
  2. I add Service Reference ot it, and one of the methods returns type that is fully compatible with TypeA(properties, name).
  3. Add Service Reference generates proxy, but it recreate new TypeA within.

On the step 3 I need proxy that will return TypeA. Not new TypeA.

Mike Chaliy
  • 25,801
  • 18
  • 67
  • 105
  • possible duplicate of ["Reuse existing types" is ignored when adding a service reference](http://stackoverflow.com/questions/134064/reuse-existing-types-is-ignored-when-adding-a-service-reference) – Alex Angas Aug 16 '10 at 01:41

3 Answers3

13

If I understand what you want to do, then it's an scenario I commonly run into, and well, WCF does have a decent answer to: Just don't use SvcUtil / WS Service Reference wizards.

If you have most of the contract classes already defined in your client side (be that because you have a shared assembly or because you have equivalent classes defined on your project), you might as well just go to the next step and either import the complete service contract in code form or simply redefine it on the client side.

There's nothing that forces you to use svcutil and friends, just define your interface and either use the channel model directly (i.e. ChannelFactory<T> and friends) or, if you prefer to use proxy classes, just create your own ClientBase<T>-derived class. It's really very easy and it will save you trouble in the long run.

tomasr
  • 13,683
  • 3
  • 38
  • 30
6

There is an easy way to share types between client and service, just by adding reference to shared type assembly to your client BEFORE adding the service reference.

You can find the detailed scenario and sample project there:

http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types-between-server-and-client.html

Walter Almeida
  • 227
  • 3
  • 2
0

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:

  1. Use svcutil with /t:metadata on each url.
  2. Rename all the generated files with something unique for the service (e.g. Rename lala.xsd to 1_lala.xsd)
  3. Copy all generated files to a single location
  4. 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;
}
#>   
Arnaud
  • 430
  • 1
  • 3
  • 13