Saturday, June 19, 2010

ServiceHostFactory in WCF

You may wonder what is the use of ServiceHostFactor class in WCF.

A quick answer is the class is created only for IIS and WAS to invoke a derived ServiceHost implemented by you. Take a look at @ServiceHost used by service.svc file which included the following attributes:

Service = "Service"
Factory = "Factory"
Debug = "Debug"
Language = "Language"
CodeBehind = "CodeBehind"

It only allows you to specify the type of the service, but not the service host. So if you have implemented a derived service host class with some overriden methods , it has not way to let IIS or WAS to invoke it.
A workaround is implementing a ServiceHostFactory class, and in the CreateServiceHost method, it can create the an instance of the derived service host class implemented by you. Note @ServiceHost does allow you to specify service host factory type, and by this, IIS and WAS can hook up with your derived service host class when it creates the service.

Jonathan