Adding Rollbar to ASP.NET Core 2+: “Some services are not able to be constructed” and “Unable to resolve service for type ‘Microsoft.AspNetCore.Http.IHttpContextAccessor'”

I’m adding Rollbar to a new ASP.NET Core 5 (which is Core 3+) API and following the guide at https://docs.rollbar.com/docs/integrating-with-aspnet-core-2-and-newer.

After adding the required code I received the following exception when starting the API:

System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Rollbar.NetCore.AspNet.RollbarLoggerProvider Lifetime: Singleton ImplementationType: Rollbar.NetCore.AspNet.RollbarLoggerProvider': Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Rollbar.NetCore.AspNet.RollbarLoggerProvider'.)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at Lotus.API.Integration.Program.Main(String[] args) in D:\WorkspacesLotusAI\Management\Lotus.API.Integration\Program.cs:line 16

This exception was originally thrown at this call stack:
[External Code]

Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Rollbar.NetCore.AspNet.RollbarLoggerProvider Lifetime: Singleton ImplementationType: Rollbar.NetCore.AspNet.RollbarLoggerProvider': Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Rollbar.NetCore.AspNet.RollbarLoggerProvider'.

Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Rollbar.NetCore.AspNet.RollbarLoggerProvider'.

A search for “Unable to resolve service for type ‘Microsoft.AspNetCore.Http.IHttpContextAccessor’ while attempting to activate ‘Rollbar.NetCore.AspNet.RollbarLoggerProvider’” lead me to answers at https://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc.

The accepted answer revealed “IHttpContextAccessor is no longer wired up by default, you have to register it yourself” with a sample services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); to be added in the ConfigureServices()  method of the StartUp class. Another answer points out “as of .NET Core 2.1 there is an extension method” that has been added to do the same thing: services.AddHttpContextAccessor();.

 

This is also yet another example of the craziness and breaking changes between the different versions of .NET Core. And while I’m thankful I’m only now making the jump from .NET Framework to .NET Core/5, it well highlights the difficulties developers face when entering the industry and looking up documentation, or when finally make a step up from earlier version – i.e. it looks like even steps from Core 2 to 2.1, and .NET 2 to 3 will give developers some unexpected surprises.