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 inserviceHostBase.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)));
}
}
Tags: REST, StructureMap, WCF
[...] we start I would like to mention that I had a lot of help from both Jimmy Bogard’s post and PlanB Software’s post (thanks for you [...]
Thanks for this article. Is it possible for you to upload the solution as well?
To tell truth I’m a newbie to StructureMap and I would like to use for a scenario where there are multiple Service projects with each project potentially having multiple service contract implementation (such as PingService, CustomerService, AssetService, CalculationService) and finally a Web application that has all the service files (i.e. multiple .svc files)
I want to use StructureMap on WCF so that but have it all registered in one place (i.e. inside web application) and I don’t know how it works.