Implementing and configuring "ServiceHostFactoryBase"

Damir Dobric Posts

Next talks:



Follow me on Twitter: #ddobric




ServiceHostFactory provides the base implementation for the factory that allows application code to participate in the creation of service hosts in managed hosting environments where the host instance is created dynamically in response to incoming messages. This is useful when hosting the service which requires some initialization data, depending on the requested .SVC page. For example, you have a service which maintain customer data for one mandator. To enable this you could use different .SVC files for each mandator: service1.svc and service2.svc. Now in SVC file you could specify the database connection string for target mandator.
In usual case the SVC file would look like:
In this case the BusService is the service implementation.
However, following code snippet shows the content of one SVC file, which configures the custom host:
<%@ServiceHost language=c# Debug="true" Factory="MyCustomHost" Service="Data Source=DADO-NB1;Initial Catalog=ServiceBusEventing;Integrated Security=True"%>
As you see, two new attributes are used: Factory and Service. The Factory defines the type which derives from ServiceHostFactory  and implements the custom host. The Service is any string which can be be useful to perform the initialization of the host. In a case of mandator, the custom host implementation could look like:
public class MyCustomHost : ServiceHostFactoryBase
public override System.ServiceModel.ServiceHostBase
CreateServiceHost(string constructorString,

[] baseAddresses) 
if (String.IsNullOrEmpty(constructorString))
throw new ApplicationException("..."); 

      // Value of attribute 'Service' in SVC file.

string myConfig = constructorString;

ServiceHost serviceBusHost =
      new ServiceHost(typeof(BusService), baseAddresses);

      return serviceBusHost; 

Posted Jan 08 2008, 11:14 PM by Damir Dobric
Filed under: is a .Net Community Blog powered by daenet GmbH.