How would I switch the implementation of the service contract at runtime?
Say I have:
[ServiceContract]
public interface IService {
[OperationContract]
DoWork();
}
public class ServiceA : IService {
public string DoWork() {
// ....
}
}
public class ServiceB : IService {
public string DoWork() {
// ....
}
}
I’d like to be able to switch the implementation that is being used from say a config file or a value in a database between the two. Would it also be possible to do this while the WCF service is hot?
You need to write a servicebehavior by implementing IServiceBehavior, and initialize the service instance using an instance provider. The following initializes a new service instance, you may implement a different logic:
And have your instance provider class implement IInstanceProvider and return related instance in GetInstance method.
Then all you need is to add servicebehaviour to service; something like