<tfoot id='tMffl'></tfoot>
  • <i id='tMffl'><tr id='tMffl'><dt id='tMffl'><q id='tMffl'><span id='tMffl'><b id='tMffl'><form id='tMffl'><ins id='tMffl'></ins><ul id='tMffl'></ul><sub id='tMffl'></sub></form><legend id='tMffl'></legend><bdo id='tMffl'><pre id='tMffl'><center id='tMffl'></center></pre></bdo></b><th id='tMffl'></th></span></q></dt></tr></i><div id='tMffl'><tfoot id='tMffl'></tfoot><dl id='tMffl'><fieldset id='tMffl'></fieldset></dl></div>

      <bdo id='tMffl'></bdo><ul id='tMffl'></ul>

        <small id='tMffl'></small><noframes id='tMffl'>

        <legend id='tMffl'><style id='tMffl'><dir id='tMffl'><q id='tMffl'></q></dir></style></legend>

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

        时间:2023-06-08
      1. <small id='VqBPU'></small><noframes id='VqBPU'>

            <bdo id='VqBPU'></bdo><ul id='VqBPU'></ul>
          • <i id='VqBPU'><tr id='VqBPU'><dt id='VqBPU'><q id='VqBPU'><span id='VqBPU'><b id='VqBPU'><form id='VqBPU'><ins id='VqBPU'></ins><ul id='VqBPU'></ul><sub id='VqBPU'></sub></form><legend id='VqBPU'></legend><bdo id='VqBPU'><pre id='VqBPU'><center id='VqBPU'></center></pre></bdo></b><th id='VqBPU'></th></span></q></dt></tr></i><div id='VqBPU'><tfoot id='VqBPU'></tfoot><dl id='VqBPU'><fieldset id='VqBPU'></fieldset></dl></div>
                  <tbody id='VqBPU'></tbody>
                <tfoot id='VqBPU'></tfoot>

              • <legend id='VqBPU'><style id='VqBPU'><dir id='VqBPU'><q id='VqBPU'></q></dir></style></legend>

                  本文介绍了如何在 ASP.NET Core 1.0 的 DI 中的 Startup 类中添加 IHttpContextAccessor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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(IServiceProvidersp) 在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(类型服务类型)在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 |微软视窗 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>();
                  

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

                  这篇关于如何在 ASP.NET Core 1.0 的 DI 中的 Startup 类中添加 IHttpContextAccessor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Unity 与 ASP.NET Core 和 MVC6 (Core) 下一篇:如何在 ASP.NET core rc2 中禁用浏览器缓存?

                  相关文章

                  • <bdo id='9FcAV'></bdo><ul id='9FcAV'></ul>

                  <tfoot id='9FcAV'></tfoot>

                  <small id='9FcAV'></small><noframes id='9FcAV'>

                1. <i id='9FcAV'><tr id='9FcAV'><dt id='9FcAV'><q id='9FcAV'><span id='9FcAV'><b id='9FcAV'><form id='9FcAV'><ins id='9FcAV'></ins><ul id='9FcAV'></ul><sub id='9FcAV'></sub></form><legend id='9FcAV'></legend><bdo id='9FcAV'><pre id='9FcAV'><center id='9FcAV'></center></pre></bdo></b><th id='9FcAV'></th></span></q></dt></tr></i><div id='9FcAV'><tfoot id='9FcAV'></tfoot><dl id='9FcAV'><fieldset id='9FcAV'></fieldset></dl></div>

                  <legend id='9FcAV'><style id='9FcAV'><dir id='9FcAV'><q id='9FcAV'></q></dir></style></legend>