Changing WCF Service Implementation at Runtime

In this post, I will show an approach that lets WCF to have two implementations for a WCF service. The proposed approach provides the ability to switch among the service implementations at runtime

Last week, I work on a service oriented project that has two types of clients. Both of the types should be able to access the services on the server and both of the types should see the same interface on the server, but the services on the server should provide basic functionality for the clients in the first group (majority of the clients) and they should provide the complete functionality for the clients in the second group, (minority of the clients). Base on the logic behind the business, some of the clients in the first group have been upgraded to the second group and some time, some of the clients in the second group have been moved to the first group.

The simple solution that came to mind is putting an if-else in each method of the services like this:

If (server should do basic functionality)
{
   basic functionality.
}
Else
{
   Complete functionality.
}

However this solution is not convincing. The problem is the fact that the codes of the basic and complete implementations have been tight together strongly. The ideal is having two different implementations; a basic and complete implementations. Based on the Admin decision the server will use the basic or complete implementations. In the time that I seek for a solution, I find that the idea of having two implementations per WCF service can be useful in many other situations. Sometimes you want to change the implementation of your services but you want to keep the old services in case the new service gets problem. Sometimes your services interact with internet resources, but you want to have a local implementation of your services in case of losing internet connection. There are other situations that the idea would be useful too.

At first glance, I thought it doesn't possible to have two implementations for each service, but thanks to the great WCF extensibility. WCF is a fantastic platform to build service-oriented applications in the .NET platform. One of the facts about the WCF is its ability to extend its functionality. Every part of the platform can be extended, and most of the default behaviors can be replaced. The problem is how we can have two implementations for each service. Studying the WCF concepts, I realized that the solution is related to a custom IInstanceProviders. In fact, using a custom InstanceProvider, a custom ServiceBehavior, and a new ServiceHostExtention, the whole scenario comes to life. Before delving into the detail, I will show you how to use the solution.

read more


No Comments

Post Reply