在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无法解析类型的服务 尝试时'Microsoft.AspNetCore.Http.IHttpContextAccessor' 激活'MyProject.Startup'。
在 Microsoft.Extensions.Internal.ConstructorMatcher.CreateInstance(的IServiceProvider 提供者) Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(的IServiceProvider provider,Type instanceType,Object [] parameters)at Microsoft.Extensions.Internal.ActivatorUtilities.GetServiceOrCreateInstance(的IServiceProvider 提供者,类型类型)at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(的IServiceProvider 提供者,类型类型)at Microsoft.AspNetCore.Hosting.Internal.StartupLoader.LoadMethods(的IServiceProvider services,类型startupType,string environmentName)at 。Microsoft.AspNetCore.Hosting&LT;&GT; c__DisplayClass1_0.b__1(的IServiceProvider sp)at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(的ServiceProvider 提供者) Microsoft.Extensions.DependencyInjection.ScopedCallSite.Invoke(的ServiceProvider 提供者) Microsoft.Extensions.DependencyInjection.SingletonCallSite.Invoke(的ServiceProvider 提供者) Microsoft.Extensions.DependencyInjection&LT;&GT; c__DisplayClass12_0.b__0(的ServiceProvider 提供者) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型 serviceType)at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(的IServiceProvider provider,输入serviceType)at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(的IServiceProvider 提供者) Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureStartup()at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 在Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
.NET Framework X64 v4.0.30319.42000 | Microsoft.AspNetCore.Hosting 版本1.0.0-rtm-21431 | Microsoft Windows 6.1.7601 S
如何在应用程序启动期间获取新版本的IHttpContextAccessor?
答案 0 :(得分:55)
它不再是默认服务。您必须在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>();
答案 1 :(得分:0)
您可以通过这种方式添加。
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();