首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Autofac核心依赖解决异常

Autofac核心依赖解决异常
EN

Stack Overflow用户
提问于 2020-01-17 09:49:45
回答 1查看 6.5K关注 0票数 1

我想将这些依赖项注入与autofac https://github.com/MHKarami97/AspNetCoreWebApi/blob/master/WebFramework/Configuration/Identity/AddCustomServicesExtensions.cs进行比较。

对此:https://github.com/MHKarami97/AspNetCoreWebApi/blob/master/WebFramework/Configuration/AutofacConfigurationExtensions.cs

但我在运行程序中遇到了错误:

代码语言:javascript
运行
复制
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = PolicyEvaluator (ReflectionActivator), Services = [Microsoft.AspNetCore.Authorization.Policy.IPolicyEvaluator], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope --->
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DefaultAuthorizationService (ReflectionActivator), Services = [Microsoft.AspNetCore.Authorization.IAuthorizationService], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> 
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DefaultAuthorizationHandlerProvider (ReflectionActivator), Services = [Microsoft.AspNetCore.Authorization.IAuthorizationHandlerProvider], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> 
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = IAuthorizationHandler[] (DelegateActivator), Services = [System.Collections.Generic.IEnumerable`1[[Microsoft.AspNetCore.Authorization.IAuthorizationHandler, Microsoft.AspNetCore.Authorization, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = ExternallyOwned ---> 
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DynamicPermissionsAuthorizationHandler (ReflectionActivator), Services = [Microsoft.AspNetCore.Authorization.IAuthorizationHandler], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> 
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = SecurityTrimmingService (ReflectionActivator), Services = [Services.Contracts.Identity.ISecurityTrimmingService], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> 
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = MvcActionsDiscoveryService (ReflectionActivator), Services = [DNTCommon.Web.Core.IMvcActionsDiscoveryService], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> 
An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.AspNetCore.Mvc.Infrastructure.IActionDescriptorCollectionProvider)' on type 'MvcActionsDiscoveryService'. ---> Multiple custom attributes of the same type found. (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.)\",\"StackTrace\":\"   
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters, Object& decoratorTarget)\\r\\n   at Autofac.Core.Resolving.InstanceLookup.Execute()\\r\\n   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\\r\\n   at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\\r\\n   
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)\\r\\n   at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\\r\\n   
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)\\r\\n   
at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)\\r\\n   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)\\r\\n   
at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType)\\r\\n   at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType)\\r\\n   
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)\\r\\n   
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)\\r\\n   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\\r\\n   
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\\r\\n   at OwaspHeaders.Core.SecureHeadersMiddleware.Invoke(HttpContext httpContext)\\r\\n   
at Swashbuckle.AspNetCore.ReDoc.ReDocMiddleware.Invoke(HttpContext httpContext)\\r\\n   
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)\\r\\n   
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)\\r\\n   
at WebFramework.Middlewares.CustomExceptionHandlerMiddleware.Invoke(HttpContext context) in F:\\\\Project\\\\Best\\\\AspNetCoreWebApi\\\\WebFramework\\\\Middlewares\\\\CustomExceptionHandlerMiddleware.cs:line 50\

另外,如何将此代码转换为autofac?

代码语言:javascript
运行
复制
services.AddScoped<IPrincipal>(provider =>
                provider.GetRequiredService<IHttpContextAccessor>()?.HttpContext?.User ?? ClaimsPrincipal.Current);
EN

回答 1

Stack Overflow用户

发布于 2020-01-18 00:07:32

看来您使用的是InstancePerRequest而不是InstancePerLifetimeScope。我是ASP.NET经典和ASP.NET内核之间的一个有文档记录的区别

使用InstancePerLifetimeScope而不是InstancePerRequest.,在以前的ASP.NET集成中,您可以将依赖项注册为InstancePerRequest,这将确保每个ASP.NET请求只创建一个依赖实例。这是因为Autofac负责设置每个请求的生存期范围。。随着Microsoft.Extensions.DependencyInjection的引入,创建每个请求和其他子生存期作用域现在是框架提供的合格容器的一部分,因此所有子生存期作用域都被平等对待--不再有特殊的“请求级作用域”。不要注册依赖项InstancePerRequest,而是使用InstancePerLifetimeScope,您应该得到相同的行为。注如果在web请求期间创建自己的生存期作用域,则将在这些子作用域中获得一个新实例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59784872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档