229

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?

Tseng
  • 61,549
  • 15
  • 193
  • 205
YuriyP
  • 4,210
  • 4
  • 25
  • 35

7 Answers7

358

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

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Joe Audette
  • 35,330
  • 11
  • 106
  • 99
  • 11
    It is worked. Also same story with `services.AddScoped()` – YuriyP May 22 '16 at 13:09
  • 8
    Do we have an official recommendation about what the proper scope is? Should it be `Singleton`, `Scoped` or `Transient`? – Mark Vincze Jun 14 '16 at 10:59
  • 13
    Ah it's discussed here, and multiple people verifies it can safely be `Singleton`. https://github.com/aspnet/Hosting/issues/793 – Mark Vincze Jun 14 '16 at 11:35
  • When doing that, I got this error : `InvalidOperationException: Cannot consume scoped service`. Any idea ? – Robouste Sep 23 '17 at 15:24
  • This is not solving the problem. I have deployed with both services.TryAddSingleton() and services.AddSingleton(); But both continue to return the same error. – jwize May 28 '18 at 20:53
  • Why is registered as singleton? – Ivan Montilla Feb 06 '19 at 18:27
  • because you only need one per application and it is non trivial to create one so you don't want to be creating it over and over – Joe Audette Feb 06 '19 at 19:16
  • 15
    Please see the next answer about using the provided extension method `services.AddHttpContextAccessor()` which is preferred/recommended by Microsoft. – Martin Bliss May 24 '20 at 03:30
  • not working on .NET 6 ASP.NET Razor Pages template – aj go Sep 19 '22 at 13:57
  • @ajgo builder.Services.AddHttpContextAccessor(); something like this should work – LordDraagon Sep 19 '22 at 18:23
  • 1
    @LordDraagon - thank you for the suggestion, I think I found the source of the issue on my side where in I am Injecting the HttpContextAccessor instead of IHttpContextAccessor on the constructor of my IndexModel class that causes this issue. Now everything is working on my side. – aj go Sep 20 '22 at 07:20
233

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>();
SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
13

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

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Babita Tokas
  • 131
  • 1
  • 4
  • I tried this and got the same error. I tried changing it in my ConfigureServices method also and got the error still – eia92 Jan 31 '23 at 01:28
10

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>();
Shalim Ahmed
  • 259
  • 3
  • 6
1

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

Example:

builder.Services.AddHttpContextAccessor();
abolfazl sadeghi
  • 2,277
  • 2
  • 12
  • 20
Ryan Ross
  • 11
  • 1
0
services.AddScoped(sp => sp.GetService<IHttpContextAccessor>().HttpContext.Session);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Suraj Rao Sep 13 '21 at 08:26
0

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.

Mikael Puusaari
  • 854
  • 1
  • 10
  • 14