7
votes

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

1 Answers

6
votes

Use

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