gpt4 book ai didi

asp.net-core - 尝试激活时无法解析类型 'Microsoft.AspNetCore.Http.IHttpContextAccessor' 的服务

转载 作者:行者123 更新时间:2023-12-05 08:36:02 25 4
gpt4 key购买 nike

在启动文件中,我试图在构造函数本身中注入(inject) IHttpContextAccessor。

在本地运行 API 时,IHttpContextAccessor 成功注入(inject)到启动类中。但是,当我将其部署到 Azure kubernetes 服务时,它失败并报告以下异常。甚至 pod 状态也是 crashloopbackoff。

有什么建议可以解决这个问题吗?

这是启动类。

public class Startup
{
private readonly IConfiguration _configuration;
private readonly ILogger _logger;
private readonly IHttpContextAccessor _httpContextAccessor;

public Startup(IConfiguration configuration, ILogger logger, IHttpContextAccessor httpContextAccessor)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
_logger.LogProcessStart();
}
}

这是 pod 日志中的异常。

    Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' whileattempting to activate 'API.Startup'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.AspNetCore.Hosting.StartupLoader.LoadMethods(IServiceProvider hostingServiceProvider, Type startupType, String environmentName)
at Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.<>c__DisplayClass3_0.<UseStartup>b__1(IServiceProvider sp)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContextcontext, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Hosting.WebHost.EnsureStartup()
at Microsoft.AspNetCore.Hosting.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.WebHost.Initialize()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at API.Program.Main(String[] args) in /__w/1/s/src/API/Program.cs:line 18

最佳答案

您需要register IHttpContextAccessor service manually .

.net 6

builder.Services.AddHttpContextAccessor();

.net 3.1,5

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

关于asp.net-core - 尝试激活时无法解析类型 'Microsoft.AspNetCore.Http.IHttpContextAccessor' 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70981655/

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