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

前端 未结 2 1391
耶瑟儿~
耶瑟儿~ 2020-12-01 06:14

I started to convert my asp.net core RC1 project to RC2 and faced with problem that now IHttpContextAccessordoes not resolved.

For sake of simplicity I

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 06:16

    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();
    

提交回复
热议问题