gpt4 book ai didi

c# - ASP.NET Core 1.0如何在DI的Startup类中添加IHttpContextAccessor?

转载 作者:IT王子 更新时间:2023-10-29 04:40:29 24 4
gpt4 key购买 nike

在 ASP.NET Core RC 1 中,我使用以下代码来检索上下文的值(页面的完整地址)。然后我在配置中记录了这个值。

public class Startup
{
public IConfigurationRoot Configuration { get; set; }
private IHostingEnvironment CurrentEnvironment { get; set; }
private IHttpContextAccessor HttpContextAccessor { get; set; }
public Startup(IHostingEnvironment env,
IHttpContextAccessor httpContextAccessor)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

if (env.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
CurrentEnvironment = env;
HttpContextAccessor = httpContextAccessor;
}
public void ConfigureServices(IServiceCollection services)
{
...

services.AddOptions();
services.Configure<WebAppSettings>(configuration);
services.Configure<WebAppSettings>(settings =>
{
...
settings.WebRootPath = CurrentEnvironment.WebRootPath;
settings.DomainUrl = HttpContextAccessor.HttpContext.Request.Host.ToUriComponent();
});
}
}

现在我开始在 ASP.NET Core 1.0 上更新项目。但是在网站启动期间,我收到以下错误:

System.InvalidOperationException Unable to resolve service for type'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting toactivate 'MyProject.Startup'.

atMicrosoft.Extensions.Internal.ConstructorMatcher.CreateInstance(IServiceProviderprovider) atMicrosoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProviderprovider, Type instanceType, Object[] parameters) atMicrosoft.Extensions.Internal.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProviderprovider, Type type) atMicrosoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProviderprovider, Type type) atMicrosoft.AspNetCore.Hosting.Internal.StartupLoader.LoadMethods(IServiceProviderservices, Type startupType, string environmentName) atMicrosoft.AspNetCore.Hosting.<>c__DisplayClass1_0.b__1(IServiceProvidersp) atMicrosoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProviderprovider) atMicrosoft.Extensions.DependencyInjection.ScopedCallSite.Invoke(ServiceProviderprovider) atMicrosoft.Extensions.DependencyInjection.SingletonCallSite.Invoke(ServiceProviderprovider) atMicrosoft.Extensions.DependencyInjection.<>c__DisplayClass12_0.b__0(ServiceProviderprovider) atMicrosoft.Extensions.DependencyInjection.ServiceProvider.GetService(TypeserviceType) atMicrosoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProviderprovider, Type serviceType) atMicrosoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProviderprovider) atMicrosoft.AspNetCore.Hosting.Internal.WebHost.EnsureStartup() atMicrosoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

.NET Framework X64 v4.0.30319.42000 | Microsoft.AspNetCore.Hostingversion 1.0.0-rtm-21431 | Microsoft Windows 6.1.7601 S

如何在应用程序启动时获取新版本的 IHttpContextAccessor?

最佳答案

它不再是默认服务。你必须在 Startup.cs 中配置它

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();

更新:在 ASP.NET Core 2.1 中,the AddHttpContextAccessor helper extension method已添加以使用正确的生命周期(单例)正确注册 IHttpContextAccessor。所以,在 ASP.NET Core 2.1 及以上版本中,代码应该是

services.AddHttpContextAccessor();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();

来源:https://github.com/aspnet/Hosting/issues/793

关于c# - ASP.NET Core 1.0如何在DI的Startup类中添加IHttpContextAccessor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38184583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com