开发者

Ninject Di bindings using a WCF service

开发者 https://www.devze.com 2023-04-12 12:39 出处:网络
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 project

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 Glob开发者_JAVA百科al.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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消