Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate
Asked Answered
E

2

9

In Startup file, I am trying to inject the IHttpContextAccessor in constructor itself.

While running the API locally, IHttpContextAccessor is injected successfully in startup class. However when I am deploying the same to Azure kubernetes service, it fails and reported below exception. even pod state is crashloopbackoff.

Any suggestion how can I resolve this issue?

Here is the startup class.

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();
    }
}

Here is the exception from pod logs.

    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
Epicanthus answered 4/2, 2022 at 5:8 Comment(0)
A
26

You need to register IHttpContextAccessor service manually.

.net 6

builder.Services.AddHttpContextAccessor();

.net 3.1,5

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Ashelyashen answered 7/2, 2022 at 6:9 Comment(3)
thanks. this works. Along with this, I have retrieved service using getservice to access header data. var httpContextAccessor = sp.GetService<IHttpContextAccessor>();Epicanthus
I was trying to use AddHttpContextAccessor() and it wouldn't compile. Finally, I found that the extension is in the nuget package "Microsoft.AspNetCore.Http". Took me too long to figure that out.Niello
the nuget package "microsoft.aspnetcore.http" is deprecated as it is now included in the framework [#76997057Bullis
R
3

on ASP.NET 8 you can add on program.cs

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Revolver answered 20/12, 2023 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.