Fix cross appdomain exception
This commit is contained in:
parent
b4b2dd6bf2
commit
9a0ea424ea
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
#if NET451
|
#if NET451
|
||||||
|
using System;
|
||||||
using System.Runtime.Remoting;
|
using System.Runtime.Remoting;
|
||||||
using System.Runtime.Remoting.Messaging;
|
using System.Runtime.Remoting.Messaging;
|
||||||
#elif NETSTANDARD1_3
|
#elif NETSTANDARD1_3
|
||||||
|
|
@ -13,7 +14,7 @@ namespace Microsoft.AspNetCore.Http
|
||||||
public class HttpContextAccessor : IHttpContextAccessor
|
public class HttpContextAccessor : IHttpContextAccessor
|
||||||
{
|
{
|
||||||
#if NET451
|
#if NET451
|
||||||
private const string LogicalDataKey = "__HttpContext_Current__";
|
private static readonly string LogicalDataKey = "__HttpContext_Current__" + AppDomain.CurrentDomain.Id;
|
||||||
|
|
||||||
public HttpContext HttpContext
|
public HttpContext HttpContext
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// 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.AspNetCore.Http.Features;
|
||||||
using Microsoft.Extensions.ObjectPool;
|
using Microsoft.Extensions.ObjectPool;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
@ -34,5 +35,31 @@ namespace Microsoft.AspNetCore.Http
|
||||||
var context = contextFactory.Create(new FeatureCollection());
|
var context = contextFactory.Create(new FeatureCollection());
|
||||||
contextFactory.Dispose(context);
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue