Skip to content

Don't re-use DefaultHttpContext if IHttpContextAccessor is in use #15049

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

Merged
merged 2 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Hosting/Hosting/src/Http/DefaultHttpContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public DefaultHttpContextFactory(IServiceProvider serviceProvider)
_serviceScopeFactory = serviceProvider.GetRequiredService<IServiceScopeFactory>();
}

internal IHttpContextAccessor HttpContextAccessor => _httpContextAccessor;

public HttpContext Create(IFeatureCollection featureCollection)
{
if (featureCollection is null)
Expand Down
8 changes: 8 additions & 0 deletions src/Hosting/Hosting/src/Internal/HostingApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ public void DisposeContext(Context context, Exception exception)
if (_defaultHttpContextFactory != null)
{
_defaultHttpContextFactory.Dispose((DefaultHttpContext)httpContext);

if (_defaultHttpContextFactory.HttpContextAccessor != null)
{
// Clear the HttpContext if the accessor was used. It's likely that the lifetime extends
// past the end of the http request and we want to avoid changing the reference from under
// consumers.
context.HttpContext = null;
}
}
else
{
Expand Down
Loading