Last week, I was updating an application from .NET Core 1.1.2 to .NET 5. One of the errors I noticed when building the application was: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'.

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'

It took me a few minutes before I remembered that IHttpContextAccessor is not wired up by default. So, I added the following in my services by doing this:

1
services.AddHttpContextAccessor();

which is the equivalent of doing this:

1
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

I know that many of the .NET Core applications have been updated by now, but if you still have an old one lingering as I did, be sure to properly update.