WCF Dependency Injection with Ninject

Home / Programming / WCF Dependency Injection with Ninject
Share...Tweet about this on TwitterShare on Facebook1Share on Google+2Share on StumbleUpon1Share on LinkedIn0Flattr the authorPin on Pinterest0Share on Reddit0Share on Tumblr0Digg this

I usually use Microsoft Unity as the IoC container as I am quite familiar with it and I like the way it can be configured, however in my new projects I started using Ninject as it seems a stable product and the community around it is quite active.

As I am building a new WCF based SOAP services, I needed a way to configure the dependency injection, and with Ninject this seem to be very easy.

Installation

First things first. In order to use Ninject in your application you would need to reference the libraries. By using NuGet you have to install

which will automatically install the Ninject (core) project.

Configuration

Once the NuGet references are installed, we need to do a couple of things.

Application service file: Service.svc needs to be changed so that the Factory is specified and it has to contain Ninject.Extensions.Wcf.NinjectServiceHostFactory as shown below.

<%@ ServiceHost Language="C#"
                Debug="true"
                Service="ServiceNamespace.Service1"
                CodeBehind="Service1.svc.cs"
                Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory"
%>

Global.asax file needs to inherit from the NinjectHttpApplication class and need to override the CreateKernel() method.

public class Global : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new NinjectDependencyResolver());
    }
}

 
once this is done, there are two ways of configuring the references:

Assuming that the Service1 expects the IRepository object to be injected…

public class Service1: IService1
{
    private IRepository _repository;
    public Service1(IRepository repository)
    {
        _repository = repository;
    }
}

we can configure the references in two ways really: in the Global.asax file directly, or by creating a new NinjectModule

If we use the implementation in the Global class:

public class Global : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind().To().InRequestScope();
        return kernel;
    }
}

as an alternative… if we use a new (bootstrap) module we put all of configuration:

public class Global : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        return StandardKernel(new NinjectDependencyResolver());
    }
}

public class NinjectDependencyResolver : NinjectModule
{
    public override void Load()
    {
        Bind().To().InRequestScope();
    }
}

That’s all folks!

    Share...Tweet about this on TwitterShare on Facebook1Share on Google+2Share on StumbleUpon1Share on LinkedIn0Flattr the authorPin on Pinterest0Share on Reddit0Share on Tumblr0Digg this

    I'm a Software Developer and Solution Architect interested in Software Development, Object-Oriented Design and Software Architecture all this especially bound to the Microsoft.NET platform. Feel free to contact me or know more in the about section

    2 Comments

    Leave a Reply

    ahalt-rpo@mailxu.com