Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 9a0ea42

Browse files
committed
Fix cross appdomain exception
1 parent b4b2dd6 commit 9a0ea42

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Microsoft.AspNetCore.Http/HttpContextAccessor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
#if NET451
5+
using System;
56
using System.Runtime.Remoting;
67
using System.Runtime.Remoting.Messaging;
78
#elif NETSTANDARD1_3
@@ -13,7 +14,7 @@ namespace Microsoft.AspNetCore.Http
1314
public class HttpContextAccessor : IHttpContextAccessor
1415
{
1516
#if NET451
16-
private const string LogicalDataKey = "__HttpContext_Current__";
17+
private static readonly string LogicalDataKey = "__HttpContext_Current__" + AppDomain.CurrentDomain.Id;
1718

1819
public HttpContext HttpContext
1920
{

test/Microsoft.AspNetCore.Http.Tests/HttpContextFactoryTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.AspNetCore.Http.Features;
56
using Microsoft.Extensions.ObjectPool;
67
using Microsoft.Extensions.Options;
@@ -34,5 +35,31 @@ public void AllowsCreatingContextWithoutSettingAccessor()
3435
var context = contextFactory.Create(new FeatureCollection());
3536
contextFactory.Dispose(context);
3637
}
38+
39+
#if NET451
40+
private static void DomainFunc()
41+
{
42+
var accessor = new HttpContextAccessor();
43+
Assert.Equal(null, accessor.HttpContext);
44+
accessor.HttpContext = new DefaultHttpContext();
45+
}
46+
47+
[Fact]
48+
public void ChangingAppDomainsDoesNotBreak()
49+
{
50+
// Arrange
51+
var accessor = new HttpContextAccessor();
52+
var contextFactory = new HttpContextFactory(new DefaultObjectPoolProvider(), Options.Create(new FormOptions()), accessor);
53+
var domain = AppDomain.CreateDomain("newDomain");
54+
55+
// Act
56+
var context = contextFactory.Create(new FeatureCollection());
57+
domain.DoCallBack(DomainFunc);
58+
AppDomain.Unload(domain);
59+
60+
// Assert
61+
Assert.True(ReferenceEquals(context, accessor.HttpContext));
62+
}
63+
#endif
3764
}
3865
}

0 commit comments

Comments
 (0)