ServiceHostFactory Class
.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)
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

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.

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;

        }

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

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.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Bad example      JocularJoe   |   Edit   |   Show History

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 What's this?: Add a tag
Flag as ContentBug
Simple implementation      m_lh ... Stanley Roark   |   Edit   |   Show History

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>" %>
Processing
Page view tracker