WCF Service Inheritance

I have a VERY sophisticated service node that consists of several DUPLEX services. They provide some common features (Connect, Disconnect, KeepAlive, etc.), but also they provide very specific functionality.

All my services inherit from a common base class (annotation).

So, I am also responsible for part of the client application, and I want the administrative bureaucratic processing of connecting, disconnecting, continuing to test and reconnecting (etc.) to be processed in the base class, so I can follow the DRY principle AND force other developers NOT Perform your own connection processing.

Is there any way to show WCF the base class of the service so that I can create a common wrapper class for bureaucratic waste of time in a client application?

I really do not want each developer of future client components to create their own shell?

And if you allow the disclosure:

Why, why, why is Microsoft so careless about the best practices and guidelines for developing clean code? This is the same as with this INotifyPropertyChanged material, where everyone is forced to write masses of unnecessary repetitive code instead of having a simple attribute to notify properties ...

+4
source share
1 answer

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.

+4
source

All Articles