Are there any risks to injecting IHttpContextAccessor into a class registered as a singleton?

aethdae 6 Reputation points
2022-04-21T21:01:29.103+00:00

I understand that IHttpContextAccessor is registered as a singleton when services.AddHttpContextAccessor() is called, and that it uses AsyncLocal to access information for the current user.

In just about all the examples I've seen, services dependent on IHttpContextAccessor are being registered as scoped or transient. Is there a reason for this or is it safe to call register services that depend on IHtppContextAccessor as singletons?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,397 questions
{count} vote

3 answers

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,106 Reputation points Microsoft Vendor
    2022-04-22T05:21:35.607+00:00

    Hi @aethdae ,

    services dependent on IHttpContextAccessor are being registered as scoped or transient. Is there a reason for this or is it safe to call register services that depend on IHtppContextAccessor as singletons?

    For this issue, I think it depends on the service lifetimes:

    Transient lifetime services are created each time they're requested from the service container. In apps that process requests, transient services are disposed at the end of the request.

    Scoped services are created once per client request (connection). In apps that process requests, scoped services are disposed at the end of the request.

    Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance. In apps that process requests, singleton services are disposed when the ServiceProvider is disposed on application shutdown. Because memory is not released until the app is shut down, consider memory use with a singleton service.

    From the above description, we can know that Transient and Scoped services are created based on the request and will be disposed at the end of the request. But the Singleton service is not, it will disposed when the Service Provider is released when the application closes. So if you register all your services as singletons, you might cause out of memory issues.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


  2. Bruce (SqlWork.com) 61,491 Reputation points
    2022-04-22T17:29:32.853+00:00

    IHttpContextAccessor will not work with a singleton. it only works within the scope of a request. Its main use is for middleware. when code in the request scope calls the singleton, it should pass the HttpContext in the call as a parameter.


  3. Michael Taylor 51,346 Reputation points
    2024-06-14T14:03:49.2733333+00:00

    Yes you can reference it in a singleton object (or any object for that matter). In general you can use a singleton (the highest lifetime) in any other object. The singleton is either created at the point it is registered or on first use and is available thereafter.

    0 comments No comments