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

前端 未结 2 1371
耶瑟儿~
耶瑟儿~ 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<IHttpContextAccessor, HttpContextAccessor>();
    
    0 讨论(0)
  • 2020-12-01 06:35

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

    services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    
    0 讨论(0)
提交回复
热议问题