• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTcz

Related Code
WCF Service factory for using Microsoft Unity 2.0 to create an instance of a Service

Dependency Injection-enabled WCF Instance Provider

When you want your IIS Hosted WCF service (.svc) to be automatically wired up to a DI container

2
1.16k 0 1 2 0 0

A very common thing you want to do when creating WCF services is to hook them up to a Dependency Injection container (such as Unity, nInject, Structure Map etc). Thankfully, WCF is quite easy to extend - and this is one way of doing it:

The first thing you need is the actual InstanceProvider. This will do the buildup. I'm using the ServiceLocator library from Microsoft P&P.

public class InjectedInstanceProvider : IInstanceProvider
{
    private readonly Type _serviceType;

    public InjectedInstanceProvider(Type serviceType)
    {
        _serviceType = serviceType;
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return GetInstance(instanceContext, null);
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return ServiceLocator.Current.GetInstance( _serviceType );
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
    }
}

The next thing you need is a Service Behavior that makes sure that the new Instance Provider is being used.

public class InjectedBehavior : IServiceBehavior
{
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach(var cdb in serviceHostBase.ChannelDispatchers)
        {
            var cd = cdb as ChannelDispatcher;
            if (cd == null)
                continue;

            foreach (var endpoint in cd.Endpoints)
            {
                endpoint.DispatchRuntime.InstanceProvider =
                    new InjectedInstanceProvider(serviceDescription.ServiceType);
            }
        }
    }
}

You'll need a Service Host that applies the behavior:

public class InjectedServiceHost : ServiceHost
{
    protected InjectedServiceHost()
    {
    }

    public InjectedServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses)
    {
    }

    protected override void OnOpening()
    {
        Description.Behaviors.Add(new InjectedBehavior());
        base.OnOpening();
    }
}

And a Service Host Factory that creates the new Host:

public class InjectedServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return new InjectedServiceHost(serviceType, baseAddresses);
    }
}

And in order to use the new Service Host Factory, your .svc file should include the Factory attribute:

<%@ ServiceHost Language="C#" Debug="true" Service="MyServiceType" Factory="MyNamespace.InjectedServiceHostFactory" %>

Finally, you'll need to wire up the Dependency Injection container to the ServiceLocator. But I'll leave that bit for a separate post.


Share: twitter | facebook   Action: used | fork | flag

dependency-injection

Mark 'dependency-injection' tag as 'like'

Mark 'dependency-injection' tag as 'ignore'

service-locator

Mark 'service-locator' tag as 'like'

Mark 'service-locator' tag as 'ignore'

wcf

Mark 'wcf' tag as 'like'

Mark 'wcf' tag as 'ignore'


 @CodingInsomnia "First one to share code"
349
July 11, 2010 3:51 PM
edited July 15, 2010 11:42 PM

Fork

 Dependency Injection-enabled WCF Instance Provider -  @CodingInsomnia Sunday 11, 2010 3:51 PM


3 Feedback

We're using a SL-framework to get a DI-framework make our framework work. :D -  @abratland Sunday 11, 2010 7:54 PM
Dependency injection is your friend :) -  @luisabreu Monday 12, 2010 9:46 PM
So basically the samething that the Common Service Factory does? =) http://commonservicefactory.codeplex.com/ -  @TheCodeJunkie Monday 12, 2010 10:12 PM

You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010