views:

146

answers:

2

Hi, my first question so hope it is suitable -

Shared interface assembly - I have a 'shared' assembly which has an interface, let's call it IDocRepository. It's marked with [ServiceContract] and there are several [OperationContract]-marked methods.

WCF implementation assemblies - I have two WCF service projects, each referencing the shared assembly, each implementing that interface as a WCF service.

Consumer assembly - Finally, I have a 'client' project, also referencing the shared assembly, with a reference to each of the two WCF services.

However, the service references generated in the consumer assembly derive from an auto-generated version of the interface:

public partial class ExampleClient : System.ServiceModel.ClientBase<SomeNamespace.ExampleSvcRef.IDocRepository>, SomeNamespace.ExampleSvcRef.IDocRepository {

What I expected - I would have hoped that both references would instead automatically inherit the interface I defined, that the consumer/client assembly is also referencing. Kind of like the re-use of classes that it provides for parameter and return types, but for the service interface.

Why - So that I can create an instance of either service reference proxy and cast it to my interface type.

-

So I could modify the generated code by hand each time, but there should be better way...?

(edit: I do have 'Reuse types in referenced assemblies' and 'Reuse types in all referenced assemblies' options selected for both service references)

+2  A: 

When you create the service reference, there is a box you can tick to make it reuse the shared definitions. Make sure the client project is already referencing the shared assembly, add the service reference again, and check all the options carefully.

If it still doesn't work, check the binding you use. I have a vague recollection that basic HTTP binding won't support re-using of types?

David M
Hi David, thanks for the comment! -I have done exactly that, I should have mentioned. :) 'Reuse types in referenced assemblies' is checked, and I have tried it with 'Reuse types in all referenced assemblies', and 'Reuse types in specified referenced assemblies' while manually picking the assembly in question - both with the same result (as above).
Kieren Johnstone
Only problem with that route is if you want to reference 2 services with the same shared lib; you get duplicated objects.
eschneider
@eschneider - I don't see how that's the case, that's what the 'Reuse...' option does, it stops the duplication of classes ('objects') by using the existing referenced classes - doesn't it? However I am seeing duplicated interfaces, which is the problem I have described in my question :).
Kieren Johnstone
I'm saying that duplication can happen (a warning); not that it's the problem in your case.
eschneider
+2  A: 

"Reuse types in referenced assemblies" only allows you to reuse Data Contracts, not Service Contracts. If you want to share Service Contracts, you don't need to use "Add Service Reference" at all. You can just use ChannelFactory directly.

// Supply the binding and address in code
Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://tempuri.org/address");
IServiceContract channel = ChannelFactory<IServiceContract>.CreateChannel(binding, address);

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

The channel object will also implement ICommunicationObject, so you can cast it if you need to call methods like Open() or Close().

Quartermeister
Great solution, thanks
Kieren Johnstone
I was going with the second option, but I get the error that there must be an EndpointAddress specified. I have an endpoint set up in the config file, with the same contract (interface) specified, but it doesn't seem to be looking there? Can you help?
Kieren Johnstone
Nevermind; you need to pass the endpoint configuration name into the ChannelFactory<> constructor.
Kieren Johnstone