首页 新闻 会员 周边

ASP.NET MVC AutoMapper 的 InstancePerRequest 问题

1
悬赏园豆:20 [已解决问题] 解决于 2015-07-14 11:58

我在AutoMapper里用的InstancePerRequest作为生命周期,但是会报错

No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.

 

我按照http://docs.autofac.org/en/latest/faq/per-request-scope.html,这里说的实现了ILifetimeScopeProvider接口SimpleLifetimeScopeProvider ,并且在初始化的时候注册了

  var provider= new SimpleLifetimeScopeProvider(container);
  var resolver = new AutofacDependencyResolver(container, provider);
  DependencyResolver.SetResolver(resolver);

依旧不行,这是为什么,应该怎么弄?

Mango_的主页 Mango_ | 初学一级 | 园豆:5
提问于:2015-07-13 10:51
< >
分享
最佳答案
1

需要使用AutofacDependencyResolver.Current.RequestLifetimeScope.Resolve<T>()获取

Mango_ | 初学一级 |园豆:5 | 2015-07-14 11:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册