Setting IHttpContextAccessor in CreateHttpContext #405
This commit is contained in:
parent
0394987271
commit
b72f95bdb5
|
|
@ -9,9 +9,18 @@ namespace Microsoft.AspNet.Hosting.Builder
|
||||||
{
|
{
|
||||||
public class HttpContextFactory : IHttpContextFactory
|
public class HttpContextFactory : IHttpContextFactory
|
||||||
{
|
{
|
||||||
|
private IHttpContextAccessor _httpContextAccessor;
|
||||||
|
|
||||||
|
public HttpContextFactory(IHttpContextAccessor httpContextAccessor)
|
||||||
|
{
|
||||||
|
_httpContextAccessor = httpContextAccessor;
|
||||||
|
}
|
||||||
|
|
||||||
public HttpContext CreateHttpContext(IFeatureCollection featureCollection)
|
public HttpContext CreateHttpContext(IFeatureCollection featureCollection)
|
||||||
{
|
{
|
||||||
return new DefaultHttpContext(featureCollection);
|
var httpContext = new DefaultHttpContext(featureCollection);
|
||||||
|
_httpContextAccessor.HttpContext = httpContext;
|
||||||
|
return httpContext;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -87,7 +87,6 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
|
|
||||||
var logger = _applicationServices.GetRequiredService<ILogger<HostingEngine>>();
|
var logger = _applicationServices.GetRequiredService<ILogger<HostingEngine>>();
|
||||||
var contextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>();
|
var contextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>();
|
||||||
var contextAccessor = _applicationServices.GetRequiredService<IHttpContextAccessor>();
|
|
||||||
var diagnosticSource = _applicationServices.GetRequiredService<DiagnosticSource>();
|
var diagnosticSource = _applicationServices.GetRequiredService<DiagnosticSource>();
|
||||||
var server = ServerFactory.Start(_serverFeatures,
|
var server = ServerFactory.Start(_serverFeatures,
|
||||||
async features =>
|
async features =>
|
||||||
|
|
@ -95,7 +94,6 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
var httpContext = contextFactory.CreateHttpContext(features);
|
var httpContext = contextFactory.CreateHttpContext(features);
|
||||||
httpContext.ApplicationServices = _applicationServices;
|
httpContext.ApplicationServices = _applicationServices;
|
||||||
var requestIdentifier = GetRequestIdentifier(httpContext);
|
var requestIdentifier = GetRequestIdentifier(httpContext);
|
||||||
contextAccessor.HttpContext = httpContext;
|
|
||||||
if (diagnosticSource.IsEnabled("Microsoft.AspNet.Hosting.BeginRequest"))
|
if (diagnosticSource.IsEnabled("Microsoft.AspNet.Hosting.BeginRequest"))
|
||||||
{
|
{
|
||||||
diagnosticSource.Write("Microsoft.AspNet.Hosting.BeginRequest", new { httpContext = httpContext });
|
diagnosticSource.Write("Microsoft.AspNet.Hosting.BeginRequest", new { httpContext = httpContext });
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNet.Hosting.Builder;
|
using Microsoft.AspNet.Hosting.Builder;
|
||||||
using Microsoft.AspNet.Http.Features;
|
using Microsoft.AspNet.Http.Features;
|
||||||
|
using Microsoft.AspNet.Http.Internal;
|
||||||
using Microsoft.AspNet.Owin;
|
using Microsoft.AspNet.Owin;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -11,13 +12,27 @@ namespace Microsoft.AspNet.Hosting.Tests
|
||||||
{
|
{
|
||||||
public class HttpContextFactoryFacts
|
public class HttpContextFactoryFacts
|
||||||
{
|
{
|
||||||
|
[Fact]
|
||||||
|
public void CreateHttpContextSetsHttpContextAccessor()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var accessor = new HttpContextAccessor();
|
||||||
|
var contextFactory = new HttpContextFactory(accessor);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var context = contextFactory.CreateHttpContext(new FeatureCollection());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.True(ReferenceEquals(context, accessor.HttpContext));
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Mutable_FeatureCollection_Wrapped_For_OwinFeatureCollection()
|
public void Mutable_FeatureCollection_Wrapped_For_OwinFeatureCollection()
|
||||||
{
|
{
|
||||||
var env = new Dictionary<string, object>();
|
var env = new Dictionary<string, object>();
|
||||||
var contextFactory = new HttpContextFactory();
|
var contextFactory = new HttpContextFactory(new HttpContextAccessor());
|
||||||
var context = contextFactory.CreateHttpContext(new FeatureCollection(new OwinFeatureCollection(env)));
|
var context = contextFactory.CreateHttpContext(new FeatureCollection(new OwinFeatureCollection(env)));
|
||||||
|
|
||||||
// Setting a feature will throw if the above feature collection is not wrapped in a mutable feature collection.
|
// Setting a feature will throw if the above feature collection is not wrapped in a mutable feature collection.
|
||||||
context.Features.Set<ICustomFeature>(new CustomFeature(100));
|
context.Features.Set<ICustomFeature>(new CustomFeature(100));
|
||||||
Assert.Equal(100, context.Features.Get<ICustomFeature>().Value);
|
Assert.Equal(100, context.Features.Get<ICustomFeature>().Value);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue