diff --git a/src/Microsoft.AspNet.Hosting/Builder/HttpContextFactory.cs b/src/Microsoft.AspNet.Hosting/Builder/HttpContextFactory.cs index d24250405b..c86cf451b5 100644 --- a/src/Microsoft.AspNet.Hosting/Builder/HttpContextFactory.cs +++ b/src/Microsoft.AspNet.Hosting/Builder/HttpContextFactory.cs @@ -9,9 +9,18 @@ namespace Microsoft.AspNet.Hosting.Builder { public class HttpContextFactory : IHttpContextFactory { + private IHttpContextAccessor _httpContextAccessor; + + public HttpContextFactory(IHttpContextAccessor httpContextAccessor) + { + _httpContextAccessor = httpContextAccessor; + } + public HttpContext CreateHttpContext(IFeatureCollection featureCollection) { - return new DefaultHttpContext(featureCollection); + var httpContext = new DefaultHttpContext(featureCollection); + _httpContextAccessor.HttpContext = httpContext; + return httpContext; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index 8c603b6e3c..4e175806e8 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -87,7 +87,6 @@ namespace Microsoft.AspNet.Hosting.Internal var logger = _applicationServices.GetRequiredService>(); var contextFactory = _applicationServices.GetRequiredService(); - var contextAccessor = _applicationServices.GetRequiredService(); var diagnosticSource = _applicationServices.GetRequiredService(); var server = ServerFactory.Start(_serverFeatures, async features => @@ -95,7 +94,6 @@ namespace Microsoft.AspNet.Hosting.Internal var httpContext = contextFactory.CreateHttpContext(features); httpContext.ApplicationServices = _applicationServices; var requestIdentifier = GetRequestIdentifier(httpContext); - contextAccessor.HttpContext = httpContext; if (diagnosticSource.IsEnabled("Microsoft.AspNet.Hosting.BeginRequest")) { diagnosticSource.Write("Microsoft.AspNet.Hosting.BeginRequest", new { httpContext = httpContext }); diff --git a/test/Microsoft.AspNet.Hosting.Tests/HttpContextFactoryFacts.cs b/test/Microsoft.AspNet.Hosting.Tests/HttpContextFactoryFacts.cs index a355ca0842..02f3a2cc2c 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HttpContextFactoryFacts.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HttpContextFactoryFacts.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using Microsoft.AspNet.Hosting.Builder; using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Owin; using Xunit; @@ -11,13 +12,27 @@ namespace Microsoft.AspNet.Hosting.Tests { 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] public void Mutable_FeatureCollection_Wrapped_For_OwinFeatureCollection() { var env = new Dictionary(); - var contextFactory = new HttpContextFactory(); + var contextFactory = new HttpContextFactory(new HttpContextAccessor()); 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. context.Features.Set(new CustomFeature(100)); Assert.Equal(100, context.Features.Get().Value);