Skip to content

Instantly share code, notes, and snippets.

@marcusoftnet
Created July 12, 2011 18:33
Show Gist options
  • Save marcusoftnet/1078629 to your computer and use it in GitHub Desktop.
Save marcusoftnet/1078629 to your computer and use it in GitHub Desktop.
An implementation to get IoC support for WCF Services using TinyIoC (shamelessly stealing from Jimmy Bogard :))
using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
/*
* This is an TinyIoC implemention of the example in the excellent blog post by Jimmy Boggard
* http://lostechies.com/jimmybogard/2008/07/30/integrating-structuremap-with-wcf/
* on how to get IoC and WCF to play together.
*
* Please read that blog post for understanding. I have included some comments here just for clarification
*
* PS
* This is just ridiculous amounts of code to get IoC working...
* DS
* */
namespace DistriktsLakare.WPUser.IoC
{
/// <summary>
/// For all of the endpoints in all of the channels, we need to give WCF our custom instance provider.
/// Also, since TinyIoC needs to know what type to create, we have to pass that information along to
/// our instance provider. This comes from the ServiceDescription passed in
/// </summary>
public class TinyIoCServiceBehavior : IServiceBehavior
{
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (var cdb in serviceHostBase.ChannelDispatchers)
{
var cd = cdb as ChannelDispatcher;
if (cd != null)
{
foreach (var ed in cd.Endpoints)
{
ed.DispatchRuntime.InstanceProvider =
new TinyIoCInstanceProvider(serviceDescription.ServiceType);
}
}
}
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
/// <summary>
/// capture the service type (passed in from our service behavior earlier),
/// then use TinyIoCContainer.Current to create an instance of that service when asked.
/// There’s no information on the InstanceContext about the service type, or we could have just used it instead.
/// </summary>
public class TinyIoCInstanceProvider : IInstanceProvider
{
private readonly Type _serviceType;
public TinyIoCInstanceProvider(Type serviceType)
{
_serviceType = serviceType;
}
public object GetInstance(InstanceContext instanceContext)
{
return GetInstance(instanceContext, null);
}
public object GetInstance(InstanceContext instanceContext, Message message)
{
return TinyIoCContainer.Current.Resolve(_serviceType);
}
public void ReleaseInstance(InstanceContext instanceContext, object instance) { }
}
/// <summary>
/// add our custom service behavior (TinyIoCServiceBehavior) to the ServiceDescription Behaviors collection,
/// right before the service host is opened in the OnOpening method.
/// </summary>
public class TinyIoCServiceHost : ServiceHost
{
public TinyIoCServiceHost() { }
public TinyIoCServiceHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses) { }
protected override void OnOpening()
{
Description.Behaviors.Add(new TinyIoCServiceBehavior());
base.OnOpening();
}
}
/// <summary>
/// override the CreateServiceHost to…create our custom service host.
/// </summary>
public class TinyIoCServiceHostFactory : ServiceHostFactory
{
public TinyIoCServiceHostFactory()
{
// This row is just an example that autoregisters everything.
// Feel free to tweak to what you need
TinyIoCContainer.Current.AutoRegister();
}
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new TinyIoCServiceHost(serviceType, baseAddresses);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment