0

I want to compare these dependency injection to autofac https://github.com/MHKarami97/AspNetCoreWebApi/blob/master/WebFramework/Configuration/Identity/AddCustomServicesExtensions.cs

to this: https://github.com/MHKarami97/AspNetCoreWebApi/blob/master/WebFramework/Configuration/AutofacConfigurationExtensions.cs

but I get error in run program:

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\

also how can I convert this code to autofac?

services.AddScoped<IPrincipal>(provider =>
                provider.GetRequiredService<IHttpContextAccessor>()?.HttpContext?.User ?? ClaimsPrincipal.Current);

1 Answer 1

1

It appears you're using InstancePerRequest rather than InstancePerLifetimeScope. This is one of the documented differences between ASP.NET classic and ASP.NET Core:

Use InstancePerLifetimeScope instead of InstancePerRequest. In previous ASP.NET integration you could register a dependency as InstancePerRequest which would ensure only one instance of the dependency would be created per HTTP request. This worked because Autofac was in charge of setting up the per-request lifetime scope. With the introduction of Microsoft.Extensions.DependencyInjection, the creation of per-request and other child lifetime scopes is now part of the conforming container provided by the framework, so all child lifetime scopes are treated equally - there’s no special “request level scope” anymore. Instead of registering your dependencies InstancePerRequest, use InstancePerLifetimeScope and you should get the same behavior. Note if you are creating your own lifetime scopes during web requests, you will get a new instance in these child scopes.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I change it to InstancePerLifetimeScope , but now when I call api for example api/v1/posts/get it's show other exception. (note : I edit post and add new exception, also I edit codes on gihtub)
I wish you'd posted a new question instead. Not only does my answer no longer make sense and I don't get credit for answering, but also there's not enough info here to diagnose the issue. Sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.