WCF service and Ninject
Recently I was looking for a way how to use Dependency Injection Container in WCF service.
This article should illustrate how to setup WCF service and make it work with Ninject DIC.
Start by creating the WCF service.
Next Step is to install Ninject NuGet packages
At this point the structure of your project should look like this:
IDataReader and DataReader is my custom object that I call from Service1 and it is actually the interface that we need to inject into Service1 constructor.
IDataReder and DataReader:
In order to inject the interface into constructor, you need to register it in Ninject container like this:
Now if you would run the application you would get this 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:
and you need to change the code accordingly:
And that is it.
I think it is very easy to set up Ninject DIC into WCF service and it can bring you a lots of benefits to have DIC in your application.
You can download whole working project here.