Why should I inject IHttpContextAccessor as a Singleton

According to comments associated with an issue raised on GitHub


In that sample, you are registering it as a singleton. Shouldn’t it be a scoped instance?

It’s fine being a singleton because the backing store is async local.

Which got a later reply


Actually if you register it as a Transient on .NET Core then it doesn’t work properly since the implementation for .NET Core is using a AsyncLocal which relies upon the instance variable to track the TLS storage slot. So it has to be registered as a singleton on .NET Core.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.