Thu , Jun 29 2023
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.
I am an engineer with over 10 years of experience, passionate about using my knowledge and skills to make a difference in the world. By writing on LifeDB, I aim to share my thoughts, ideas, and insights to inspire positive change and contribute to society. I believe that even small ideas can create big impacts, and I am committed to giving back to the community in every way I can.
Leave a Reply