可以传递 Ninject Web.Common UseNinjectMiddleware 一个 lambda 其中 returns 一个内核实例吗?
Is it ok to pass Ninject Web.Common UseNinjectMiddleware a lambda which returns a kernel instance?
为什么 UseNinjectMiddleware
采用 lambda 而不是容器实例?
这样做可以吗?
var container = CreateKernel();
app.UseNinjectMiddleware(() => container);
而不是这个:
app.UseNinjectMiddleware(() => CreateKernel);
第一个代码段中的容器实例用于解决我应用程序另一部分中的依赖项。 Ninject 中间件是否需要能够创建它自己的实例,它可以随意修改、修改和重新创建?如果不是,我想避免多次构建依赖树,因此我想知道 lambda 是否可以仅 return 实例。
有几点需要注意:
OwinBootstrapper
component will load any defined NinjectModule
's after calling the passed in Func<IKernel>
so you need to take care that you do not load anything twice. see here
Bootstrapper
component calls Dispose
on IKernel
so you will need to be sure you do not attempt to access the instance after a shutdown has been initiated. see here
您几乎可以在创建底层内核后立即访问它,也许您更愿意提出 PR 以使其可用?
目前OwinAppBuilderExtensions
定义
public const string NinjectOwinBootstrapperKey = "NinjectOwinBootstrapper";
用于存储 OwinBootstrapper
的 运行 时间实例
app.Properties.Add(NinjectOwinBootstrapperKey, bootstrapper);
OwinBootstrapper
持有对 Bootstrapper
组件的 运行 时间实例的私有引用,而 Bootstrapper
实例又公开对 运行-IKernel
的时间实例
public IKernel Kernel
{
get { return kernelInstance; }
}
所以添加一点 PR
public IKernel Kernel
{
get
{
this.bootstrapper.Kernel;
}
}
到 OwinBootstrapper
可以在调用 app.UseNinjectMiddleware(() => container);
.
后通过 app.Properties
使基础 IKernel
可供您的代码使用
为什么 UseNinjectMiddleware
采用 lambda 而不是容器实例?
这样做可以吗?
var container = CreateKernel();
app.UseNinjectMiddleware(() => container);
而不是这个:
app.UseNinjectMiddleware(() => CreateKernel);
第一个代码段中的容器实例用于解决我应用程序另一部分中的依赖项。 Ninject 中间件是否需要能够创建它自己的实例,它可以随意修改、修改和重新创建?如果不是,我想避免多次构建依赖树,因此我想知道 lambda 是否可以仅 return 实例。
有几点需要注意:
OwinBootstrapper
component will load any defined NinjectModule
's after calling the passed in Func<IKernel>
so you need to take care that you do not load anything twice. see here
Bootstrapper
component calls Dispose
on IKernel
so you will need to be sure you do not attempt to access the instance after a shutdown has been initiated. see here
您几乎可以在创建底层内核后立即访问它,也许您更愿意提出 PR 以使其可用?
目前OwinAppBuilderExtensions
定义
public const string NinjectOwinBootstrapperKey = "NinjectOwinBootstrapper";
用于存储 OwinBootstrapper
app.Properties.Add(NinjectOwinBootstrapperKey, bootstrapper);
OwinBootstrapper
持有对 Bootstrapper
组件的 运行 时间实例的私有引用,而 Bootstrapper
实例又公开对 运行-IKernel
public IKernel Kernel
{
get { return kernelInstance; }
}
所以添加一点 PR
public IKernel Kernel
{
get
{
this.bootstrapper.Kernel;
}
}
到 OwinBootstrapper
可以在调用 app.UseNinjectMiddleware(() => container);
.
app.Properties
使基础 IKernel
可供您的代码使用