Skip to content

Instantly share code, notes, and snippets.

@ewilde
Created January 24, 2013 15:14
Show Gist options
  • Save ewilde/4622835 to your computer and use it in GitHub Desktop.
Save ewilde/4622835 to your computer and use it in GitHub Desktop.
Configure #wcf for use with structure map. Code based on http://lostechies.com/jimmybogard/2008/07/30/integrating-structuremap-with-wcf/
/// <summary>
/// Resolves instances using the structure map object factory.
/// </summary>
public class StructureMapInstanceProvider : IInstanceProvider
{
/// <summary>
/// The service type
/// </summary>
private readonly Type serviceType;
/// <summary>
/// Initializes a new instance of the <see cref="StructureMapInstanceProvider" /> class.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
public StructureMapInstanceProvider(Type serviceType)
{
this.serviceType = serviceType;
}
/// <summary>
/// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext" /> object.
/// </summary>
/// <param name="instanceContext">The current <see cref="T:System.ServiceModel.InstanceContext" /> object.</param>
/// <returns>
/// A user-defined service object.
/// </returns>
public object GetInstance(InstanceContext instanceContext)
{
// ReSharper disable AssignNullToNotNullAttribute
return this.GetInstance(instanceContext, message: null);
// ReSharper restore AssignNullToNotNullAttribute
}
/// <summary>
/// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext" /> object.
/// </summary>
/// <param name="instanceContext">The current <see cref="T:System.ServiceModel.InstanceContext" /> object.</param>
/// <param name="message">The message that triggered the creation of a service object.</param>
/// <returns>
/// The service object.
/// </returns>
public object GetInstance(InstanceContext instanceContext, Message message)
{
return ObjectFactory.GetInstance(this.serviceType);
}
/// <summary>
/// Called when an <see cref="T:System.ServiceModel.InstanceContext" /> object recycles a service object.
/// </summary>
/// <param name="instanceContext">The service's instance context.</param>
/// <param name="instance">The service object to be recycled.</param>
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
}
}
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
/// <summary>
/// Service behavior to create instances using the structure map container.
/// </summary>
public class StructureMapServiceBehavior : IServiceBehavior
{
/// <summary>
/// Provides the ability to inspect the service host and the service description to confirm that the service can run successfully.
/// </summary>
/// <param name="serviceDescription">The service description.</param>
/// <param name="serviceHostBase">The service host that is currently being constructed.</param>
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
/* No implementation */
}
/// <summary>
/// Provides the ability to pass custom data to binding elements to support the contract implementation.
/// </summary>
/// <param name="serviceDescription">The service description of the service.</param>
/// <param name="serviceHostBase">The host of the service.</param>
/// <param name="endpoints">The service endpoints.</param>
/// <param name="bindingParameters">Custom objects to which binding elements have access.</param>
public void AddBindingParameters(
ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints,
BindingParameterCollection bindingParameters)
{
/* No implementation */
}
/// <summary>
/// Provides the ability to change run-time property values or insert custom extension objects such as error handlers, message or parameter interceptors, security extensions, and other custom extension objects.
/// </summary>
/// <param name="serviceDescription">The service description.</param>
/// <param name="serviceHostBase">The host that is currently being built.</param>
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
var channelDispatcher = channelDispatcherBase as ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
{
endpointDispatcher.DispatchRuntime.InstanceProvider =
new StructureMapInstanceProvider(serviceDescription.ServiceType);
}
}
}
}
}
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using StructureMap;
/// <summary>
/// An implementation of a <see cref="ServiceHost"/> that uses structure map to create WCF services.
/// </summary>
public class StructureMapServiceHost : ServiceHost
{
/// <summary>
/// Initializes a new instance of the <see cref="StructureMapServiceHost" /> class.
/// </summary>
public StructureMapServiceHost()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="StructureMapServiceHost" /> class.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
/// <param name="baseAddresses">The base addresses.</param>
public StructureMapServiceHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
}
/// <summary>
/// Invoked during the transition of a communication object into the opening state.
/// </summary>
protected override void OnOpening()
{
Description.Behaviors.Add(new StructureMapServiceBehavior());
base.OnOpening();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment