WCF using Ninject Dispose not triggering in requestscope

房东的猫 提交于 2019-12-08 18:46:46

问题


Here is my module

internal class WebServiceConfiguration : NinjectModule
{
    public override void Load()
    {
        Bind<IWebService>().To<WebService>().InRequestScope();
    }
}

Here is the global.asax

public class Global : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new WebServiceConfiguration());
    }
}

I also tried InScope(c => OperationContext.Current)

Here is my service with IDisposable in IWebService

[ServiceBehavior(InstanceContextMode = InstanceContextModeDefinition.Mode)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WebService : IWebService
{
    private readonly ISomeService _someService;

    public WebService(ISomeService someService)
    {
        _someService = someService;
    }

    public void Dispose()
    {
        _someService.Dispose();
    }

Here is the ServiceHostFactory in the service markup

<%@ ServiceHost Language="C#" Debug="true" Factory="Ninject.Extensions.Wcf.NinjectDataServiceHostFactory" Service="WCFTest.Services.WebService" CodeBehind="WebService.svc.cs" %>

The injection of dependencies works. My only concern is that the dispose method is not being triggered when the Client closes the service call.

I tried to remove the Factory="Ninject.Extensions.Wcf.NinjectDataServiceHostFactory" just to test if the Dipose will be called, and it did call but of course i won't have auto injection. So there might be something i'm doing wrong in the setup? or there is a bug on ninject not calling Dispose?

Note: I grab the sample setup in ninject wcf extension and just added some DI.

Your help will be appreciated.

BTW: I'm using Ninject 3.0.0.15, Ninject.Extensions.Wcf 3.0.0.5, Ninject.Web.Common 3.0.0.7


回答1:


Use

Bind<IWebService, WebService>().To<WebService>().InRequestScope();


来源:https://stackoverflow.com/questions/10529592/wcf-using-ninject-dispose-not-triggering-in-requestscope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!