HardLuck
HardLuck

Reputation: 1577

Ninject with WCF service - Dispose() not fired

I have a WCF service which has some lower level services as constructor parameters, thay are injected via Ninject and have a db context in them. All constructor parameters are IDisposable. The service looks like this:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DirectoryService : IDirectoryService
    {
        private IDomainService DomainService { get; set; }

        public DirectoryService(IDomainService domainService)
        {
            DomainService = domainService;
        }
        ...
    }

I would like to dispose these instances after each method call complete, I've tried to set up my bindings as follows:

        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IDomainService>().To<DomainService>().InScope(x => OperationContext.Current);
            ...
        }

also like this:

            private static void RegisterServices(IKernel kernel)
            {
                kernel.Bind<IDomainService>().To<DomainService>().InScope(x => HttpContext.Current);
                ...
            }

and like this:

                private static void RegisterServices(IKernel kernel)
                {
                    kernel.Bind<IDomainService>().To<DomainService>().InRequestScope();
                    ...
                }

still none of the options really disposes my items after a successfull call to the service, any ideas are welcome, thank you in advance.

Upvotes: 2

Views: 547

Answers (1)

BatteryBackupUnit
BatteryBackupUnit

Reputation: 13243

AFAIR .InScope() (HttpContext.Current or OperationContext.Current) both work - but not how you would expect it. Ninject will keep a WeakReference to the scope object and periodically check whether it was collected. If it was collected, it will dispose the object in it's scope. If there's no memory pressure this can take a long time.

InRequestScope(); should work if you've got Ninject.Extensions.Wcf installed. But beware, you also need to make sure you've got the latest version of Ninject.Web.Common - 3.2.3 because there was an issue with a previous version where .InRequestScope() didn't work.

Upvotes: 1

Related Questions