Setting up ninject 3.2 on a WCF service

This is a step by step guide on how to create a WCF Service using Ninject to resolve (inject) its dependencies.

We start from scratch by creating a New C# WCF Service Project and end with an example of the service being instantiated with its dependencies resolved by Ninject. The code that resulted from this guide can be found here.

1. Create the WCF service project

new-wcf-service-project

2. Run Install-Package Ninject.Web.Common.WebHost from the package manager console.

  1. Make sure that you are using NuGet 1.6 or higher. Using an older version will result in missing references.
  2. Figured out this step after reading this.

3. Run Install-Package Ninject.Extensions.Wcf

  1. Figured this step out by looking at the Ninject examples on github

4. Teak your svc’s markup so the ServiceHost element has this attribute: Factory=”Ninject.Extensions.Wcf.NinjectServiceHostFactory”

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

You’re all done!

To see it in action, let’s create something to inject:

public class Repository : IRepository
{
    public IEnumerable<Widget> GetWidgets()
    {
        return new [] {
            new Widget {Name = "Widget1", Price=11},
            new Widget {Name = "Widget2", Price=22}
        };
    }
}
public interface IRepository
{
    IEnumerable<Widget> GetWidgets();
}
public class Widget
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

We now need to add the Ninject binding in App_Start\NinjectWebCommon.cs. This file was generated when you installed the Ninject.Web.Common.WebHost NuGet package. At the bottom of the file you’ll find the RegisterServices method, add the binding there:

...
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IRepository>().To<Repository>();
}  
...      

Let’s make Service1 (automatically generated when you created the new WCF Service project) use the Repository:

public class Service1 : IService1
{
    private IRepository _repo;
    public Service1(IRepository repo)
    {
        _repo = repo;
    }
    public string TellMeHowManyWidgets()
    {
        return string.Format(
            "We have {0} widgets.", _repo.GetWidgets().Count());
    }

Remember to add the new method to the service interface as well:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string TellMeHowManyWidgets();
    ...

We’re finally ready to see this working. Select the Service1.svc file on the Solution Explorer and hit F5, this will make the WCF Test Client appear. Double click the TellMeHowManyWidgets method and hit the Invoke button.

ninject-generated-wcf-servcie

There it is! Our service replied with “We have 2 widgets”. This proves that the IRepository dependency was fulfilled by Ninject with a Repository object.

Advertisements