Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C#

WCF Service and Ninject

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
9 May 2016CPOL1 min read 18.6K   11   5
Dependecy injection with ninja

Introduction

Recently, I was looking for a way in which to use Dependency Injection Container in WCF service.

For my MVC projects, I usually use Unity, but I wasn’t able to make it work for WCF service, so I was looking for an alternative and I found it is very easy to use Ninject.

This article should illustrate how to setup WCF service and make it work with Ninject DIC.

WCF Service

Start by creating the WCF service.

create service

Setup Ninject

Next step is to install Ninject NuGet packages:

Capture2

ninject ninject-webhost

At this point, the structure of your project should look like this:

ninjectwebcommon

IDataReader is my custom object that I call from Service1 and it is actually the interface that we need to inject into Service1 constructor. DataReader is implementation of this interface.

Service1

C#
//interface
[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);
}

//service
public class Service1 : IService1
{
    IDataReader _reader;
    public Service1(IDataReader reader)
    {
        _reader = reader;
    }

    public string GetData(int value)
    {
        return _reader.ReadData(value);
    }
}

IDataReader and DataReader

C#
//interface
public interface IDataReader
{
    string ReadData(int value);
}

//class
public class DataReader : IDataReader
{

    public string ReadData(int value)
    {
        return string.Format("Data for value: {0} is {1}", value, new Guid());
    }
}

In order to inject the interface into constructor, you need to register it in Ninject container like this:

C#
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IDataReader>().To<DataReader>();
}

Now, if you would run the application, you would get this error:

error

The problem is that you’ve just set up the container, but you didn’t wire it into the application.

Wire the Ninject into the Application

You need to change the Service settings.

You can do this by markup:

viewmarkup

and you need to change the code accordingly:

C#
//original
<%@ ServiceHost Language="C#" Debug="true" 
Service="WcfDemo.Service1" CodeBehind="Service1.svc.cs" %>

//with ninject factory
<%@ ServiceHost Language="C#" 
Debug="true" Service="WcfDemo.Service1" 
CodeBehind="Service1.svc.cs" 
Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory" %>

Summary

I think it is very easy to set up Ninject DIC into WCF service and it can bring you a lot of benefits to have DIC in your application.

You can download the entire working project here.

This article was originally posted at http://codedreaming.com/wcf-service-and-ninject

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseNice Pin
Júnior Pacheco10-May-16 8:54
professionalJúnior Pacheco10-May-16 8:54 
GeneralRe: Nice Pin
xszaboj10-May-16 10:20
xszaboj10-May-16 10:20 
SuggestionImages Pin
hoernchenmeister9-May-16 21:06
hoernchenmeister9-May-16 21:06 
GeneralRe: Images Pin
xszaboj9-May-16 21:14
xszaboj9-May-16 21:14 
GeneralRe: Images Pin
hoernchenmeister9-May-16 21:58
hoernchenmeister9-May-16 21:58 
Thanks for the quick fixing. I'll look into it Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.