4
votes

I've been trying to get the Dispose method on my IDisposable WCF service called whilst using Ninject's NinjectServiceHost without any luck. I've then downloaded the Ninject.extensions.WCF example code and tried to get the IDisposable TimeService's Dispose() method to be called, but it does not get called either.

The service is instantiated correctly, just the Dispose() doesn't get called.

Is this a bug or something that myself and the example code are missing?

I've created a stripped down service and testing host that reproduces the issue. The code is below.

I'm using Ninject 3.0.1.10, Ninject.extensions.WCF 3.0.0.5, .net 4.5

ServiceModule.cs code (for setting up bindings)

using Ninject.Modules;

namespace TestNinjectWcf
{
   public class ServiceModule : NinjectModule
   {
      public override void Load()
      {
         Bind<Service1>().ToSelf(); 
         // I've also tried Bind<IService1>().To<Service1>()
         // and also tried various scopes such as InParent() and InRequestScope()
      }
   }
}

Console Test Program to start the service.

using System;
using Ninject.Extensions.Wcf;
using Ninject;
using TestNinjectWcf;

namespace TestConsole
{
   class Program
   {
      static void Main(string[] args)
      {
         var kernel = new StandardKernel(new ServiceModule());
         var service = kernel.Get<NinjectServiceHost<Service1>>();
         service.Open();
         Console.WriteLine("Service Started");
         Console.ReadKey();
         service.Close();
      }
   }
}

Service Implementation

using System;
using System.Diagnostics;
using System.ServiceModel;

namespace TestNinjectWcf
{
   [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
   public class Service1 : IService1, IDisposable
   {
      public Service1()
      {
         Debug.WriteLine("Constructor");
      }

      public string GetData(int value)
      {
         return string.Format("You entered: {0}", value);
      }

      public void Dispose()
      {
         Debug.WriteLine("Dispose");  // This line never gets called!
      }
   }
}
1

1 Answers

0
votes

Maybe it is that you have created singleton service ? (InstanceContextMode.Single) Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created