Asp.net 上下文中每个Owin的对象范围

Asp.net 上下文中每个Owin的对象范围,asp.net,ninject,owin,ninject-extensions,Asp.net,Ninject,Owin,Ninject Extensions,我们有一个非常基本的Owin应用程序,我们希望使用Ninject来解决依赖关系。我想指出的是,没有涉及Web API、MVC或其他中间件 我们设置了如下内核: private static StandardKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind<Test>().ToSelf().InRequestScope(); kernel.

我们有一个非常基本的Owin应用程序,我们希望使用Ninject来解决依赖关系。我想指出的是,没有涉及Web API、MVC或其他中间件

我们设置了如下内核:

private static StandardKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Test>().ToSelf().InRequestScope();
        kernel.Load(Assembly.GetExecutingAssembly());
        return kernel;
    }
我真的不知道这是不是一种方式,但在文档中找不到类似的东西

我们想要的是在Owin请求的范围内解析单个测试实例。假设我们有以下中间件:

 app.Use((context,next) =>
            {
               // How to access my Ninject Kernel or our test instance here?
                return next.Invoke();
            });


 app.Use((context,next) =>
                {
                   // How to access the same Ninject Kernel or our Test instance here?
                    return next.Invoke();
                });

有人知道如何做到这一点吗?

在研究了NInject代码之后,我觉得NInject.Web.Common.OwinHost在没有WebApi或者其他附加组件的情况下是不应该工作的

事实上,InScope只有在某些组件实现INinjectHttpApplicationPlugin时才能工作,如下所示,并且该接口目前仅在特定扩展中实现,如MVC、WebApi或WCF


所以我建议唯一的方法是实现您自己的INinjectHttpApplicationPlugin。在中有一些想法,可能会为NInject的简单范围鉴别器提供一个构建块。

好的,所以我找到了它

当我将Ninject.Web和Ninject.Web.Common NuGet包添加到我的项目中时,一切都按预期进行

现在ik可以使用.InRequestScope(),并且每个请求都会创建一次(并共享)我的所有依赖项。此外,IDisposable的所有实现都已正确处置。它的工作原理与使用WebAPI或MVC时一样

 app.Use((context,next) =>
            {
               // How to access my Ninject Kernel or our test instance here?
                return next.Invoke();
            });


 app.Use((context,next) =>
                {
                   // How to access the same Ninject Kernel or our Test instance here?
                    return next.Invoke();
                });