[英]Mixing Simple Injector Scopes in MVC, Web API and OWIN
I would like to migrate my setup with Ninject to SimpleInjector. 我想将Ninject的设置迁移到SimpleInjector。 At the moment I have a custom library which is referenced from - an ASP.NET MVC application - an ASP.NET Web API application 目前,我有一个自定义库,该库引用自-ASP.NET MVC应用程序-ASP.NET Web API应用程序
This library has a NinjectModule implementation which features some declaration like the following 该库具有NinjectModule实现,该实现具有一些类似于以下内容的声明
Bind<MyDataContext>().To<MyDataContext>().InRequestScope();
My question is relative to the InRequestScope() lifetime. 我的问题是相对于InRequestScope()生命周期的。 As I read on the SimpleInjector documentation, for the ASP.NET MVC application is suggested to use WebRequestLifestyle
as the DefaultScopedLifestyle
option while is suggested AsyncScopedLifestyle for the Web API . 正如我在SimpleInjector文档上所读的,对于ASP.NET MVC应用程序 ,建议将WebRequestLifestyle
用作DefaultScopedLifestyle
选项,而对于Web API则建议使用AsyncScopedLifestyle。
Also when using Owin, which is my case in both the apps, the docs suggests to wrap everything within a block like 同样在使用Owin时(这在两个应用程序中都是我的情况),文档建议将所有内容包装在一个块中,例如
app.Use(async (context, next) => {
using (AsyncScopedLifestyle.BeginScope(container)) {
await next();
}
});
For my understanding this means: 以我的理解,这意味着:
Lifestyle.Scoped
lifestyle 每次注册都应在Lifestyle.Scoped
注册。 app.Use( ... )
code block 两个应用程序都应允许通过app.Use( ... )
代码块解析作用域实例 Is this correct? 这个对吗?
every registration should be registered with the Lifestyle.Scoped lifestyle 每次注册都应在Lifestyle中注册。
All registrations that would have been InRequestScope
in Ninject can now be registered as Lifestyle.Scoped in Simple Injector. 现在,所有在Ninject中本来是InRequestScope
注册都可以在Simple Injector中注册为Lifestyle.Scoped。
Both the applications should allow scoped instances to be resolved through the app.Use( ... ) code block 两个应用程序都应允许通过app.Use(...)代码块解析作用域实例
When the app.Use( ... )
code block with the AsyncScopedLifestyle
is applied to all requests, this means that MVC code can run within an Async Scope as well. 当app.Use( ... )
与所述码块AsyncScopedLifestyle
被施加到所有的请求,这意味着MVC代码可以一个异步范围内运行为好。 This means that you don't need the WebApiRequestLifestyle
anymore and can use AsyncScopedLifestyle
for your MVC application as well. 这意味着您不再需要WebApiRequestLifestyle
,并且也可以将AsyncScopedLifestyle
用于您的MVC应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.