YuriyP
YuriyP

Reputation: 4478

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'

I started to convert my asp.net core RC1 project to RC2 and faced with problem that now IHttpContextAccessordoes not resolved.

For sake of simplicity I created new ASP.NET RC2 project using Visual Studio Template ASP.NET Core Web Application (.Net Framework). Than I added constructor for HomeController which template created for me.

public HomeController(IHttpContextAccessor accessor)
{
}

And after I start application I receive next error:

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'TestNewCore.Controllers.HomeController'. в Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)

In my real application I need to resolve IHttpContextAccessor in my own service class for getting access to _contextAccessor.HttpContext.Authentication and to _contextAccessor.HttpContext.User. Everething works fine in RC1. So how can it suppose to be in RC2?

Upvotes: 247

Views: 186260

Answers (8)

Riccardo Cafagna
Riccardo Cafagna

Reputation: 54

I had the same issue (similar error message) even with the service registered in DI. Maybe is useful for someone, instead of using the interface I was using the actual implementation.

This doesn't work:

private readonly HttpContextAccessor _contextAccessor;

public YourController(HttpContextAccessor contextAccessor)
{
    _contextAccessor = contextAccessor;
}

You must use the interface:

private readonly IHttpContextAccessor _contextAccessor;

public YourController(IHttpContextAccessor contextAccessor)
{
    _contextAccessor = contextAccessor;
}

Upvotes: 1

Mikael Puusaari
Mikael Puusaari

Reputation: 1054

For anyone that comes here and made the woopsie of injecting HttpContextAccessor instead of IHttpContextAccessor in the constructor of the class where you want to use it.

This same error is thrown and the inner exception will point to the class with the problem.

Upvotes: 15

Ryan Ross
Ryan Ross

Reputation: 31

You can now use the AddHttpContextAccessor extension method from Microsoft.Extensions.DependencyInjection

Example:

builder.Services.AddHttpContextAccessor();

Upvotes: 3

Shalim Ahmed
Shalim Ahmed

Reputation: 375

For .NET Core 7.0, add the following code in the Program.cs class:

builder.Services.AddHttpContextAccessor();

This is equivalent to:

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Upvotes: 15

Babita Tokas
Babita Tokas

Reputation: 211

For .NET Core 6.0, add the following in the Program.cs class:

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 

Upvotes: 21

A.M.Rashed Mahamud
A.M.Rashed Mahamud

Reputation: 53

services.AddScoped(sp => sp.GetService<IHttpContextAccessor>().HttpContext.Session);

Upvotes: 1

SpruceMoose
SpruceMoose

Reputation: 10320

As of .NET Core 2.1 there is an extension method that has been added to correctly register an IHttpContextAccessor as a singleton. See Add helper to register IHttpContextAccessor #947. Simply add as follows in your ConfigureServices() method:

services.AddHttpContextAccessor();

This is equivalent to:

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Upvotes: 254

Joe Audette
Joe Audette

Reputation: 36736

IHttpContextAccessor is no longer wired up by default, you have to register it yourself

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Upvotes: 391

Related Questions