InvalidOperationException:无法解析“Microsoft.AspNetCore.Http.IHttpContextAccessor”类型的服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37371264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:47:51  来源:igfitidea点击:

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

.netdependency-injectionasp.net-core

提问by YuriyP

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

我开始将我的 asp.net core RC1 项目转换为 RC2 并面临现在IHttpContextAccessor没有解决的问题。

For sake of simplicity I created new ASP.NET RC2 project using Visual Studio Template ASP.NET Core Web Application (.Net Framework). Than I added constructor for HomeController which template created for me.

为简单起见,我使用 Visual Studio Template 创建了新的 ASP.NET RC2 项目ASP.NET Core Web Application (.Net Framework)。比我为 HomeController 添加了构造函数,它为我创建了模板。

public HomeController(IHttpContextAccessor accessor)
{
}

And after I start application I receive next error:

在我启动应用程序后,我收到下一个错误:

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'TestNewCore.Controllers.HomeController'. в Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)

InvalidOperationException:尝试激活“TestNewCore.Controllers.HomeController”时无法解析“Microsoft.AspNetCore.Http.IHttpContextAccessor”类型的服务。• Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)

In my real application I need to resolve IHttpContextAccessorin my own service class for getting access to _contextAccessor.HttpContext.Authenticationand to _contextAccessor.HttpContext.User. Everething works fine in RC1. So how can it suppose to be in RC2?

在我的实际应用程序中,我需要IHttpContextAccessor在我自己的服务类中解析以访问_contextAccessor.HttpContext.Authentication和访问_contextAccessor.HttpContext.User. Everething 在 RC1 中运行良好。那么它怎么可能出现在 RC2 中呢?

回答by Joe Audette

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

IHttpContextAccessor 不再默认绑定,需要自己注册

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

回答by CalC

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

从 .NET Core 2.1 开始,添加了一个扩展方法以将 正确注册IHttpContextAccessor为单例。请参阅添加帮助程序以注册 IHttpContextAccessor #947。只需在您的ConfigureServices()方法中添加以下内容:

services.AddHttpContextAccessor();

This is equivalent to:

这相当于:

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();