3

I recently created a WCF service library. I am planning on hosting it in IIS. Since I want to reuse my repository layer I decided to go use Ninject in my WCF service as well (I use it in other projects in the solution).

I installed the Ninject Wcf Extensions. I configured it using the NinjectServiceHostFactory in the svc file. I added a Global.asax file to override the CreateKernel() that inherits from NinjectWcfApplication but I am not sure if I am using the bindings correctly. I first started with:

Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();

But I quickly realized that this does not work since no data was saved to my database. It appears that the WCF service does not use the ASP.NET pipeline. I went ahead and tried both of these as well just to see if my data was committed to the database:

Bind<IUnitOfWork>().To<UnitOfWork>().InThreadScope();
Bind<IUnitOfWork>().To<UnitOfWork>().InTransientScope();

No luck. I then decided to try:

Bind<IUnitOfWork>().To<UnitOfWork>().InSingletonScope();

This worked but I don't want my database context to be shared by every single request that comes in to the WCF service. I then did some research and found the following approach:

Bind<IUnitOfWork>().To<UnitOfWork>().InScope(c => OperationContext.Current);

This works but is it correct? I wan t something to resemble the InRequestScope for a MVC application. Each request to the service should get its own Database context.

Thomas
  • 5,888
  • 7
  • 44
  • 83

2 Answers2

3

I suggest to have a look at the latest build from the CI-Server http://teamcity.codebetter.com

You need Ninject, Ninject.Web.Common, Ninject.Extensions.Wcf

With this version you can use InRequestScope for Wcf.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Thanks Remo, I will take a look. But assume I just want to use Ninject and Ninject.Extensions.Wcf, would OperationContext.Current be something you suggest using as scope. Especially if I would want to move the WCF service to a non IIS host down the line. – Thomas Oct 13 '11 at 19:43
  • Probably you missunderstood. You will have to do this soon anyway if you want to use the latest version of Ninject. Web.cmmon is a required dependency for the wcf extension even if self hosted. – Remo Gloor Oct 13 '11 at 21:40
1

I am new to Ninject, but I can tell you that OperationContext.Current is the equivalent to HttpContext.Current for web application.

So your first thought was to use .InRequestScope(); (which is equivalent to .InScope(c => HttpContext.Current);)

so I guess that using .InScope(c => OperationContext.Current); for WCF is pretty correct.

Baptiste Pernet
  • 3,318
  • 22
  • 47
  • 1
    Nothing really wrong with this, just wanted to point out there's also a mode in WCF, where it piggybacks onto a `HttpContext.Current`, though in general `OperationContext.Current` is right. Note that @Remo Gloor's recommendation is the way to go as it wraps this stuff up, dealing cleanly with scenarios like mixed WCF/Web apps, regardless of whether you've told WCF to create a HttpContext or not (as opposed to maintaining two completely different contexts). – Ruben Bartelink Oct 12 '11 at 19:42