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

编程入门 行业动态 更新时间:2024-10-24 10:20:01
本文介绍了如何在ASP.NET Core 1.0的DI中的Startup类中添加IHttpContextAccessor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在ASP.NET Core RC 1中,我使用以下代码来检索context的值(页面的完整地址).然后,我将此值记录在配置中.

In ASP.NET Core RC 1 I used the following code to retrieve the value of context (full address of the page). Then I recorded this value in the configuration.

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上更新项目.但是在网站启动期间,出现以下错误:

Now I started to update the project on ASP.NET Core 1.0. But during the launch of the site I get the following error:

System.InvalidOperationException无法解析类型的服务 尝试执行'Microsoft.AspNetCore.Http.IHttpContextAccessor' 激活"MyProject.Startup".

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

在 Microsoft.Extensions.Internal.ConstructorMatcher.CreateInstance(IServiceProvider 供应商) Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider 提供者,类型为instanceType,Object []参数)位于 Microsoft.Extensions.Internal.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider 提供者,类型类型)在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider 提供者,类型类型)在 Microsoft.AspNetCore.Hosting.Internal.StartupLoader.LoadMethods(IServiceProvider 服务,键入startupType,字符串environmentName),位于 Microsoft.AspNetCore.Hosting.<> c__DisplayClass1_0.b__1(IServiceProvider sp)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider 供应商) Microsoft.Extensions.DependencyInjection.ScopedCallSite.Invoke(ServiceProvider 供应商) Microsoft.Extensions.DependencyInjection.SingletonCallSite.Invoke(ServiceProvider 供应商) Microsoft.Extensions.DependencyInjection.<> c__DisplayClass12_0.b__0(ServiceProvider 供应商) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供者,键入serviceType),位于 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 供应商) Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureStartup()在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 在Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

at Microsoft.Extensions.Internal.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Extensions.Internal.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Microsoft.AspNetCore.Hosting.Internal.StartupLoader.LoadMethods(IServiceProvider services, Type startupType, string environmentName) at Microsoft.AspNetCore.Hosting.<>c__DisplayClass1_0.b__1(IServiceProvider sp) at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ScopedCallSite.Invoke(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.SingletonCallSite.Invoke(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.<>c__DisplayClass12_0.b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureStartup() at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at 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

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

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

How do I get the new version IHttpContextAccessor during application startup?

推荐答案

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

It is no longer a default service. You have to configure it in Startup.cs

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

更新:在ASP.NET Core 2.1中, AddHttpContextAccessor添加了辅助程序扩展方法以正确注册具有正确生命周期(单身)的IHttpContextAccessor.因此,在ASP.NET Core 2.1及更高版本中,代码应为

UPDATE: In ASP.NET Core 2.1, the AddHttpContextAccessor helper extension method was added to correctly register the IHttpContextAccessor with the correct lifetime (singleton). So, in ASP.NET Core 2.1 and above, the code should be

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

来源: github/aspnet/Hosting/issues/793

更多推荐

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

本文发布于:2023-11-17 00:07:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   如何在   NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!