C# RegisterWebRequest已过时,我可以使用Lifestyle.Scoped吗?

C# RegisterWebRequest已过时,我可以使用Lifestyle.Scoped吗?,c#,dependency-injection,simple-injector,C#,Dependency Injection,Simple Injector,我只是在检查我的精神是否正常。这是我使用simpleinjector 3.3.2的代码 Container.RegisterPerWebRequest<HttpContextBase>(() => { var context = HttpContext.Current; if (context == null && Container.IsVerifying) return new FakeHttpContext(); return new Ht

我只是在检查我的精神是否正常。这是我使用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();
然而,RegisterWebRequest现在被标记为过时,我不能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);
Container.Options.DefaultScopedLifestyle=new WebRequestLifestyle();
//所以我们可以将HttpContextBase注入到任何类中
容器。寄存器(()=>
{
var context=HttpContext.Current;
if(context==null&&Container.IsVerifying)
返回新的FakeHttpContext();
返回新的HttpContextWrapper(上下文);
},生活方式。范围);
所以我的问题是“我是否应该使用
lifesture.Scoped
来替换
registerWebRequest
,并且我仍然可以按原样使用代码



您显示的代码是正确的。
Container.Register(Func,lifiety.Scoped)
替换
Container.registerWebRequest(Func)

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);