Posts Tagged ‘WCF’

Integrating StructureMap with WCF 4.0 REST Services

Monday, August 16th, 2010

Ok first things first this post borrows a lot of code from  Jimmy Bogard’s post from a couple of years ago entitled Integrating StructureMap With WCF so credit where credits due. However there are a couple of minor changes that you need to make in order to get StructureMap to work properly with your WCF RESTful service.

 

Create A Custom Instance Provider

This code is identical to Jimmy’s original code.

public class StructureMapInstanceProvider : IInstanceProvider

{

    private readonly Type _serviceType;

 

    public StructureMapInstanceProvider(Type serviceType)

    {

        _serviceType = serviceType;

    }

 

    #region IInstanceProvider Members

 

    public object GetInstance(InstanceContext instanceContext)

    {

        return GetInstance(instanceContext, null);

    }

 

    public object GetInstance(InstanceContext instanceContext, Message message)

    {

        return ObjectFactory.GetInstance(_serviceType);

    }

 

    public void ReleaseInstance(InstanceContext instanceContext, object instance)

    {

    }

 

    #endregion

}

 

Create A Custom Service Behaviour Class

This code is identical to Jimmy’s original code.

public class StructureMapServiceBehaviour : IServiceBehavior

   {

       #region IServiceBehavior Members

 

       public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)

       {

           foreach (var endpointDispatcher in

               serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>().SelectMany(

                   channelDispatcher => channelDispatcher.Endpoints))

           {

               endpointDispatcher.DispatchRuntime.InstanceProvider =

                   new StructureMapInstanceProvider(serviceDescription.ServiceType);

           }

       }

 

       public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,

                                        Collection<ServiceEndpoint> endpoints,

                                        BindingParameterCollection bindingParameters)

       {

       }

 

       public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)

       {

       }

 

       #endregion

   }

 

Create A Custom Service Host

This code is almost identical to Jimmy’s. The only difference is that your custom class should inherit from the WebServiceHost class rather than the normal ServiceHost class for normal WCF Services.

public class StructureMapWebServiceHost : WebServiceHost

{

    public StructureMapWebServiceHost()

    {

    }

 

    public StructureMapWebServiceHost(object singletonInstance, params Uri[] baseAddresses) : base(singletonInstance, baseAddresses)

    {

    }

 

    public StructureMapWebServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses)

    {

    }

 

    protected override void OnOpening()

    {

        Description.Behaviors.Add(new StructureMapServiceBehaviour());

        base.OnOpening();

    }

}

 

Create A Custom Service Host Factory

Once again this code is almost identical to Jimmy’s. The only difference is that we inherit from WebServiceHostFactory rather than the usual ServiceHostFactory class.

/// <summary>

/// StructureMap Service Host Factory for WCF REST Services

/// </summary>

public class StructureMapWebServiceHostFactory : WebServiceHostFactory

{

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)

    {

        return new StructureMapWebServiceHost(serviceType, baseAddresses);

    }

}

 

Wiring Up Our Route

In our Global.asax file create our route using the StructureMapWebServiceHostFactory rather than the normal WebServiceHostFactory.

public class Global : HttpApplication

{

    void Application_Start(object sender, EventArgs e)

    {

        IoC.RegisterDependencies();

 

        RegisterRoutes();

    }

 

    private void RegisterRoutes()

    {       

        RouteTable.Routes.Add(new ServiceRoute("ReferenceData", new StructureMapWebServiceHostFactory(), typeof(ReferenceData)));

    }

}