Unable to resolve service for type 'Microsoft AspNetCore Http IHttpContextAccessor'
If you are facing this error while migrating .net older version to .net core 7.0 -
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Controller'. Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
then For .NET Core 7.0 please try to add the following code in the Program.cs class:
builder.Services.AddHttpContextAccessor();
This is equivalent to:
services.TryAddSingleton();
after that you can add other instance creation logic.this is because of IHttpContextAccessor is no longer wired up by default, you have to register it yourself.i hope this will help, thanks for reading.
You may also like: Authorized Commvault Distributor | Enterprise Backup Solutions
Leave a Reply
Please log in to comment on this post.