-
Notifications
You must be signed in to change notification settings - Fork 65
Prefer IHttpContextAccessor over HttpContextAccessor #471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This change will check if we're currently in a runtime environment that contains a IHttpContextAccessor; if we are, then we'll prefer that over HttpContextAccessor. This allows people to potentially customize what it means to get and set a HttpContext.
@joperezr can you approve this? sharepoint needs to be able to use a custom IHttpContextAccessor |
using Microsoft.Extensions.Options; | ||
|
||
namespace System.Web.Hosting; | ||
|
||
internal sealed class HostingEnvironmentAccessor | ||
{ | ||
private static readonly HttpContextAccessor _defaultHttpContextAccessor = new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very familiar with this code, but how often is _accessor or _current null? If not too often, should we make this be lazily evaluated instead so as to only initialize it if needed? (might also not matter if instantiating an HttpContextAccessor is cheap)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how often is _accessor or _current null
good point - most of the time they are not null. It will only be if someone is running outside of a asp.net core host. I've changed it to just be a back up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a tiny question, but looks good otherwise.
This change will check if we're currently in a runtime environment that contains a IHttpContextAccessor; if we are, then we'll prefer that over HttpContextAccessor. This allows people to potentially customize what it means to get and set a HttpContext.