views:

103

answers:

2

I am relatively new to .Net and C# development and am having an issue decoupling WCF services from an app into a network DLL I am creating. The DLL's goal is to offer a simple way to host and access a service from a server and client application and to add some functionality to the basic service for heartbeat and automatic reconnection without each application having to specify heartbeat methods in their WCF services and witho/ut having the apps manage a timer for automatic reconnection.

The DLL offers a ServiceServer and a ServiceClient class that have these goals:

ServiceServer :

  • Creates and manages the ServiceHost instance.
  • Host a service from outside the DLL (passed as a generic).
  • Add heartbeat operations to the service to be hosted, as well as other operations common to all our client/server apps.

ServiceClient :

  • Creates and makes the client service reference available to the client application. The service reference (auto-generated) is also passed as a generic from the application.
  • Add heartbeat methods to the service reference for the client, as well as other operations common to all our client/server apps.
  • Automatic reconnection using a timer or similar.

So far I have tried to use partial classes, generics and static extension methods without success. The issue is that to make my DLL completely decoupled I obtain and create the service reference and service using generics; I am unable to extend the received generic type using any of these approaches.

I am basically trying to extend the client service reference with additional methods to be able to send heartbeats and such without needing another independent connection and service (which would make the heartbeat ineffective), and without the client application having to know anything about sending heartbeats and automatic reconnection. Likewise, I want to extend the service that the server class receives as a parameter to add operations and the implementation of the server heartbeat code and eventually other common-to-all-apps methods too.

A: 

No it will not work this way.

When you define the service you first create one or more service contracts (best practice is to use interfaces). Service contract interface has to be marked with ServiceContract attribute and each exposed method used in service has to be marked with OperationContract attribute. Then you create service class which implements these interfaces. Such class can be exposed as WCF service with endpoint for each interface (service contract).

No other approach works. You can't add extension methods, use generic or whatever else to "extend" implemented service. What you can is to inherit existing service class and add additional interface. Obviously this is not no code solution unless you create some very advanced code to generate dynamic data type at runtime (= emitting MSIL at runtime).

Ladislav Mrnka
Thanks for taking the time to answer this. I'm surprised there isn't an easy way to decouple the WCF code from an application, or to host two services on an endpoint simultaneously. I guess I'm going to forget that idea.
Form
Check the answer from Pablo, it looks like I was wrong and there is solution for adding "operations" to existing service.
Ladislav Mrnka
+2  A: 

You might want to explore this solution for implemeting hearbeats automatically in a wcf services.

http://weblogs.asp.net/cibrax/archive/2010/05/17/enabling-service-availability-in-wcf-services.aspx

The solution also provides an extension method for the client.

Thanks Pablo.