diff --git a/src/Microsoft.AspNetCore.Http/HttpContextAccessor.cs b/src/Microsoft.AspNetCore.Http/HttpContextAccessor.cs index 9bb3dc375a..99be5929b6 100644 --- a/src/Microsoft.AspNetCore.Http/HttpContextAccessor.cs +++ b/src/Microsoft.AspNetCore.Http/HttpContextAccessor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #if NET451 +using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Messaging; #elif NETSTANDARD1_3 @@ -13,7 +14,7 @@ namespace Microsoft.AspNetCore.Http public class HttpContextAccessor : IHttpContextAccessor { #if NET451 - private const string LogicalDataKey = "__HttpContext_Current__"; + private static readonly string LogicalDataKey = "__HttpContext_Current__" + AppDomain.CurrentDomain.Id; public HttpContext HttpContext { diff --git a/test/Microsoft.AspNetCore.Http.Tests/HttpContextFactoryTests.cs b/test/Microsoft.AspNetCore.Http.Tests/HttpContextFactoryTests.cs index 40d3fa8935..5f5b86e4b1 100644 --- a/test/Microsoft.AspNetCore.Http.Tests/HttpContextFactoryTests.cs +++ b/test/Microsoft.AspNetCore.Http.Tests/HttpContextFactoryTests.cs @@ -1,6 +1,7 @@ // 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 System; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.ObjectPool; using Microsoft.Extensions.Options; @@ -34,5 +35,31 @@ namespace Microsoft.AspNetCore.Http var context = contextFactory.Create(new FeatureCollection()); contextFactory.Dispose(context); } + +#if NET451 + private static void DomainFunc() + { + var accessor = new HttpContextAccessor(); + Assert.Equal(null, accessor.HttpContext); + accessor.HttpContext = new DefaultHttpContext(); + } + + [Fact] + public void ChangingAppDomainsDoesNotBreak() + { + // Arrange + var accessor = new HttpContextAccessor(); + var contextFactory = new HttpContextFactory(new DefaultObjectPoolProvider(), Options.Create(new FormOptions()), accessor); + var domain = AppDomain.CreateDomain("newDomain"); + + // Act + var context = contextFactory.Create(new FeatureCollection()); + domain.DoCallBack(DomainFunc); + AppDomain.Unload(domain); + + // Assert + Assert.True(ReferenceEquals(context, accessor.HttpContext)); + } +#endif } } \ No newline at end of file