.NET Framework Class Library
ServiceHostFactory Class

Factory that provides instances of ServiceHost in managed hosting environments where the host instance is created dynamically in response to incoming messages.

Namespace:  System.ServiceModel.Activation
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
Syntax

Visual Basic (Declaration)
Public Class ServiceHostFactory _
    Inherits ServiceHostFactoryBase
Visual Basic (Usage)
Dim instance As ServiceHostFactory
C#
public class ServiceHostFactory : ServiceHostFactoryBase
Visual C++
public ref class ServiceHostFactory : public ServiceHostFactoryBase
JScript
public class ServiceHostFactory extends ServiceHostFactoryBase
Remarks

The managed hosting environments that support dynamic activation are Internet Information Services (IIS) and Windows Process Activation Service (WAS).

If you have implemented a custom derivative of ServiceHost, consider also implementing a factory that derives from the ServiceHostFactory class.

If you have implemented a custom derivative of ServiceHostBase instead, then consider also implementing a factory that derives your factory from ServiceHostFactoryBase directly.

Examples

This examples show how to use the ServiceHostFactory class:

C#
    public class DerivedFactory : ServiceHostFactory 
    { 

        protected override ServiceHost CreateServiceHost( Type t, Uri[] baseAddresses ) 
        { 
            return new DerivedHost( t, baseAddresses ); 
        }

        //Then in the CreateServiceHost method, we can do all of the
        //things that we can do in a self-hosted case:
        public override ServiceHostBase CreateServiceHost
                (string service, Uri[] baseAddresses)

        {

            // The service parameter is ignored here because we know our service.
            ServiceHost serviceHost = new ServiceHost(typeof(HelloService),
                baseAddresses);
            return serviceHost;

        }

    }
Inheritance Hierarchy

System..::.Object
  System.ServiceModel.Activation..::.ServiceHostFactoryBase
    System.ServiceModel.Activation..::.ServiceHostFactory
      System.ServiceModel.Activation..::.WebScriptServiceHostFactory
      System.ServiceModel.Activation..::.WebServiceHostFactory
      System.Web.ApplicationServices..::.ApplicationServicesHostFactory
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Tags :


Community Content

JocularJoe
Bad example

The example is bizarre and probably buggy.

It overrides "ServiceHostBase CreateServiceHost(string service, Uri[] baseAddresses)" and returns a ServiceHost instance, rather than the DerivedHost instance that one would expect.

In fact there is in most cases no need to override this method: the base implementation calls "CreateServiceHost( Type t, Uri[] baseAddresses)", which does return a DerivedHost instance.

A more sensible implementation would be:

publicclass DerivedFactory : ServiceHostFactory
{

protected override ServiceHost CreateServiceHost( Type t, Uri[] baseAddresses )
{
returnnew DerivedHost( t, baseAddresses );
}

}

Tags :

Stanley Roark
Simple implementation

Strange example, my working implementation is the following:

public class DerivedServiceHostFactory : ServiceHostFactory 
{

protected override ServiceHost CreateServiceHost( Type t, Uri[] baseAddresses ) {
return new DerivedServiceHost( t, baseAddresses );
}

}

Then if you host your service in IIS you will have the service.svc page with the following content:

<%@ ServiceHost Factory="<MyNamespace>.DerivedServiceHostFactory" Service="<MyNamespace>.<MyService>" %>

Page view tracker