gpt4 book ai didi

c# - ASP.NET MVC5 : No scope with a Tag matching 'AutofacWebRequest' is visible from the scope

转载 作者:太空狗 更新时间:2023-10-29 19:46:59 29 4
gpt4 key购买 nike

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

从请求实例的范围内看不到带有与“AutofacWebRequest”匹配的标记的范围。这通常表示注册为每个 HTTP 请求的组件正在被 SingleInstance() 组件(或类似场景)请求。在 Web 集成下,始终从 DependencyResolver.Current 或 ILifetimeScopeProvider.RequestLifetime 请求依赖项,从不从容器本身请求依赖项.

尝试转换以下 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)
);

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

最佳答案

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

关于c# - ASP.NET MVC5 : No scope with a Tag matching 'AutofacWebRequest' is visible from the scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24999968/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com