混淆WCF服务的Ninject作用域行为

时间:2018-03-16 23:46:53

标签: c# wcf ninject

似乎没有任何范围行为按预期工作。我有一个非常简单的WCF服务应用程序:

Service.cs:

public class Service : IService, IDisposable
{
    public Service()
    {
        Console.WriteLine("Service instantiated");
    }

    public void Dispose()
    {
        Console.WriteLine("Service disposed");
    }

    ...
}

ConsoleHost.cs:

internal static class ConsoleHost
{
    public static void Main(string[] args)
    {
        var kernel = new StandardKernel();

        // this line is changed to produce results below
        kernel.Bind<Service>().ToSelf().InSingletonScope();

        var host = kernel.Get<NinjectServiceHost<Service>>();

        host.Open();
        Console.ReadKey();
        host.Close();
        ((IDisposable) host).Dispose();
    }
}

当服务在Singleton范围内时,这些是2个请求后的日志:

Service instantiated // this line is immediately after the app starts
Service disposed     // this is after the first requests finishes
Service instantiated
Service disposed

因此Ninject实例化服务,并在等待第一个请求时保持服务。

在请求范围内时,会发生以下情况:

Service instantiated
Service instantiated
Service disposed
Service disposed
Service instantiated
Service disposed
Service disposed

Ninject在启动应用程序时仍会实例化服务,但现在即使在第一个请求完成后它也不会处理它。最重要的是,它似乎现在两次调用Dispose方法。

瞬态范围行为与Request范围相同,但Dispose只调用一次。这是Ninject / WCF的错误,还是我做错了什么?

0 个答案:

没有答案