我在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);
依旧不行,这是为什么,应该怎么弄?
需要使用AutofacDependencyResolver.Current.RequestLifetimeScope.Resolve<T>()获取