ASP.NET MVC5:从作用域中看不到具有匹配'AutofacWebRequest'的标记的作用域

jmm*_*312 8 c# inversion-of-control autofac asp.net-mvc-5

我在使用autofac v3.5.0,Autofac.Extras.CommonServiceLocator v3.2.0,Autofac.Mvc5 v3.3.2以及targetframework net45的ASP.NET MVC 5应用程序中遇到错误:

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.

当试图转换以下Rhino-Security windsor映射时:

WindsorServiceLocator windsorServiceLocator = new WindsorServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => windsorServiceLocator);

    container.Register(
            Component.For<IAuthorizationService>()
                .ImplementedBy<AuthorizationService>()
                .Lifestyle.Is(Lifestyle.Transient),
            Component.For<IAuthorizationRepository>()
                .ImplementedBy<AuthorizationRepository>()
                .Lifestyle.Is(Lifestyle.Transient),
            Component.For<IPermissionsBuilderService>()
                .ImplementedBy<PermissionsBuilderService>()
                .Lifestyle.Is(Lifestyle.Transient),
            Component.For<IPermissionsService>()
                .ImplementedBy<PermissionsService>()
                .Lifestyle.Is(Lifestyle.Transient)
            );
Run Code Online (Sandbox Code Playgroud)

到autofac映射.前4个映射没有任何问题,但最后一个映射导致上述错误.我已经跟踪了这行错误发生的第一个实例:https: //github.com/hibernating-rhinos/rhino-security/blob/master/Rhino.Security/Security.cs#L34

builder.RegisterType<AuthorizationService>().As<IAuthorizationService>().InstancePerDependency();
builder.RegisterType<AuthorizationRepository>().As<IAuthorizationRepository>().InstancePerDependency();
builder.RegisterType<PermissionsBuilderService>().As<IPermissionsBuilderService>().InstancePerDependency();
builder.RegisterType<PermissionsService>().As<IPermissionsService>().InstancePerDependency();

// Everything works except this
builder.RegisterType<OrganizationInformationExtractor>().As<IEntityInformationExtractor<Organization>>().InstancePerDependency();
Run Code Online (Sandbox Code Playgroud)

小智 1

检查 OrganizationInformationExtractor 需要的所有参数。这些参数之一要么未在 autofac 中注册,要么未注册为 InstancePerDependency。这对我有用。