Fix cross appdomain exception

This commit is contained in:
BrennanConroy 2016-07-19 08:47:01 -07:00
parent b4b2dd6bf2
commit 9a0ea424ea
2 changed files with 29 additions and 1 deletions

View File

@ -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
{

View File

@ -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
}
}