NinjectServiceHost in WCF service does not call Dispose ()

I am trying to get the Dispose method in my IDisposable WCF service, called when using Ninject NinjectServiceHost, with no luck. Then I downloaded the sample code Ninject.extensions.WCF and tried to call the IDisposable TimeService Dispose () method, but it was not called either.

The service is created correctly, just the call to Dispose () is not called.

Is this a bug or something that the example code is missing for me?

I created a disabled service and a test node that reproduces this problem. The code is below.

I am 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! } } } 
+4
source share
1 answer

Maybe you created a single-user service? ( InstanceContextMode.Single ) Only one InstanceContext is used for all incoming calls and is not processed after calls. If the service object does not exist, it is created

0
source

Source: https://habr.com/ru/post/1492629/


All Articles