Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

IHttpContextAccessor is no longer wired up by default, you have to register it yourself

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

As of .NET Core 2.1 there is an extension method that has been added to correctly register an IHttpContextAccessor as a singleton. See Add helper to register IHttpContextAccessor #947. Simply add as follows in your ConfigureServices() method:

services.AddHttpContextAccessor();

This is equivalent to:

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();