I don't know how a good way to get multiple WCF proxies to inherit from a common base class. You can manually edit the code generated when adding a link to the service, but this is not suitable for ease of maintenance.
But you can have multiple proxies that use the same basic interface if you share the contract assembly between the client and server. You can then write a helper class on the client side (possibly using extension methods) to perform common operations with the base interface. You can also put a helper class in a common assembly and reuse it with multiple clients.
I did not work with duplex bindings, so I'm not sure if this causes complications.
For example, suppose you have two services, Calculator and Echo, and each of them must implement the Keepalive method. You can define three interfaces to a service contract:
[ServiceContract] public interface IStatefulService { [OperationContract] void KeepAlive(int sessionID); } [ServiceContract] public interface ICalculator : IStatefulService { [OperationContract] int Add(int a, int b); } [ServiceContract] public interface IEcho : IStatefulService { [OperationContract] string Echo(string message); }
You can put the interfaces of the service contract into a shared class library that are shared by both the client and server.
On the server, you will have a ServiceBase class that will implement IStatefulService and contain keepalive processing code. You will have a specific CalculatorService that comes from ServiceBase and implements ICalculator; and a specific EchoService that comes from ServiceBase and implements IEcho.
On the client, you will need two proxies, one per service. Theoretically, you can generate a proxy using "add service link", using the "Use link types from link assemblies" checkbox, but I am having problems with this. Instead, you can simply use ChannelFactory directly, for example:
var echoer = (new ChannelFactory<IEcho>("")).CreateChannel(); Console.WriteLine(echoer.Echo("hello")); var calculator = (new ChannelFactory<ICalculator>("")).CreateChannel(); Console.WriteLine(calculator.Add(2, 3));
(In production code, you would like to reuse channel factories rather than creating them on the fly like this. And you will get rid of the proxy and you will have error handling ...)
You also need to configure the endpoints in the client configuration file.
Once you have two proxies that use the base interface, you can write a utility class that helps you work with this interface.