diff --git a/src/Microsoft.AspNet.Http.Abstractions/IHttpContextFactory.cs b/src/Microsoft.AspNet.Http.Abstractions/IHttpContextFactory.cs new file mode 100644 index 0000000000..f0e28db8a7 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Abstractions/IHttpContextFactory.cs @@ -0,0 +1,13 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Http.Features; + +namespace Microsoft.AspNet.Http +{ + public interface IHttpContextFactory + { + HttpContext CreateHttpContext(IFeatureCollection featureCollection); + void Dispose(HttpContext httpContext); + } +} diff --git a/src/Microsoft.AspNet.Http/HttpContextFactory.cs b/src/Microsoft.AspNet.Http/HttpContextFactory.cs new file mode 100644 index 0000000000..4557cd13fb --- /dev/null +++ b/src/Microsoft.AspNet.Http/HttpContextFactory.cs @@ -0,0 +1,29 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Http.Features; + +namespace Microsoft.AspNet.Http.Internal +{ + public class HttpContextFactory : IHttpContextFactory + { + private IHttpContextAccessor _httpContextAccessor; + + public HttpContextFactory(IHttpContextAccessor httpContextAccessor) + { + _httpContextAccessor = httpContextAccessor; + } + + public HttpContext CreateHttpContext(IFeatureCollection featureCollection) + { + var httpContext = new DefaultHttpContext(featureCollection); + _httpContextAccessor.HttpContext = httpContext; + return httpContext; + } + + public void Dispose(HttpContext httpContext) + { + _httpContextAccessor.HttpContext = null; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Http.Tests/HttpContextFactoryTests.cs b/test/Microsoft.AspNet.Http.Tests/HttpContextFactoryTests.cs new file mode 100644 index 0000000000..8cb2cc6e9b --- /dev/null +++ b/test/Microsoft.AspNet.Http.Tests/HttpContextFactoryTests.cs @@ -0,0 +1,25 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Http.Features; +using Xunit; + +namespace Microsoft.AspNet.Http.Internal +{ + public class HttpContextFactoryTests + { + [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)); + } + } +} \ No newline at end of file