的RegisterPerWebRequest已过时,我可以使用Lifestyle.Scoped吗?
作者:互联网
我只是在检查我的理智.这是我的代码,使用simpleinjector 3.3.2
Container.RegisterPerWebRequest<HttpContextBase>(() =>
{
var context = HttpContext.Current;
if (context == null && Container.IsVerifying) return new FakeHttpContext();
return new HttpContextWrapper(context);
});
Container.Verify();
…
public class FakeHttpContext : HttpContextBase { }
但是,RegisterPerWebRequest现在被标记为已过时,我不是100%确定这是否是更改代码以反映新代码库的正确方法.
Container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
//So we can inject HttpContextBase into any class
Container.Register<HttpContextBase>( () =>
{
var context = HttpContext.Current;
if (context == null && Container.IsVerifying)
return new FakeHttpContext();
return new HttpContextWrapper(context);
}, Lifestyle.Scoped);
所以我的问题是“我应该使用Lifestyle.Scoped代替RegisterPerWebRequest,并且我仍然可以按原样使用代码吗?
Judging by the docs I should be doing it right
解决方法:
您显示的代码是正确的. Container.Register T(Func T,Lifestyle.Scoped)替换了Container.RegisterPerWebRequest T(Func T).
标签:dependency-injection,c,simple-injector 来源: https://codeday.me/bug/20191026/1934371.html