Tuesday, February 20, 2018

ASP.NET Core - IHttpContextAccessor

ASP.NET Core introduces the IHttpContextAccessor interface as a way to provide access to the HttpContext. Before you can use it, you have to register it at application startup inside the IServicesCollection. During a code review I noticed that it was registered like this:

This turns out to be wrong. The IHttpContextAccessor should be registered as a singleton. So the correct code is the following:

More information: https://www.strathweb.com/2016/12/accessing-httpcontext-outside-of-framework-components-in-asp-net-core/

No comments: