Reuse type not reusing interface / service contract

April 4th, 2012 | Posted by Jeffrey Paarhuis in Developing

I was fighting with this all day long.

I created a WCF service project (the host) and placed the ServiceContract (Interface) in a separate assembly. In another project (the consumer) I referenced that assembly and generated a proxy, with add service reference, and made sure the “reuse types in referenced assemblies” was checked. It then generated the proxy, but not using my handmade interface. To be entirely clear: it is generating an interface, but not using mine from the referenced assembly.

That’s weird, I thought I checked reuse types? So I tried it over and over with different settings, endpoints and cups of coffee, but with no success. So at the end of the day I googled a bit and found out that the “reuse types in referenced assemblies” only applies to DataContracts and not to ServiceContracts! Doh!

So, is it a bug or is it a feature? Well, to me it makes no sense that ServiceContracts don’t get reused, so in my opinion it’s a bug.

What are our options then? Not using the “add service reference”, but doing it manually with a few lines of code, which is also a bit neater:

// Supply the binding and address in code
Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("");
IServiceContract channel = ChannelFactory.CreateChannel(binding, address);

// Or read them from the config file
ChannelFactory channelFactory = new ChannelFactory();
IServiceContract channel = channelFactory.CreateChannel();

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

4 Responses

  • Why would you want to reuse an interface class in this situation? Client and server are seperated from each other. The interface is only describing the operations which the service is supporting. Nothing fancy. So could you explain to me why you would like to do this?

    • Well, to me it seems the re-use types feature in visual studio is there because we don’t want to generate a proxy when we already have the contracts at our disposal. It does that already perfectly fine for the DataContracts but just not for the ServiceContracts.

      In practice I don’t give a sh*t if my interface is generated or not, but this is just a matter of principle. I tried to make this work for a couple of hours and finally found out this just isn’t supported, at such a moment I’m so obsessed with it that I wan’t to find out why MS did this.

      I also posted it to MSDN, final answer: You can think of it as a limitation.

  • Alexey says:

    Actually in my situation it is necessary to reuse interface. Because I want my App to have two instances of the same class 1 accessed through WCF and another locally (in referenced assembly). And for this purpose i need to use same interface.
    Unfortunately this is not a bug and not a feature at all. This must be an option but it isn’t. There is an option in a service reference properties dialog to generate asyn operations. In this case we need a new interface for wcf service class. But if i don’t intend to use async operations why can’t I use existing interface?

Leave a Reply to Johan de Koning Cancel reply

%d bloggers like this: