diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/ActionContextAccessor.cs b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/ActionContextAccessor.cs index 73332cd82d..1204730795 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/ActionContextAccessor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/ActionContextAccessor.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; #else @@ -13,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure public class ActionContextAccessor : IActionContextAccessor { #if NET451 - private static string Key = typeof(ActionContext).FullName; + private static readonly string Key = typeof(ActionContext).FullName + AppDomain.CurrentDomain.Id; public ActionContext ActionContext { diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Infrastructure/ActionContextAccessorTests.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Infrastructure/ActionContextAccessorTests.cs new file mode 100644 index 0000000000..e463e4d850 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Infrastructure/ActionContextAccessorTests.cs @@ -0,0 +1,37 @@ +// 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 Xunit; + +namespace Microsoft.AspNetCore.Mvc.Infrastructure +{ +#if NET451 + public class ActionContextAccessorTests + { + private static void DomainFunc() + { + var accessor = new ActionContextAccessor(); + Assert.Equal(null, accessor.ActionContext); + accessor.ActionContext = new ActionContext(); + } + + [Fact] + public void ChangingAppDomainsDoesNotBreak_ActionContextAccessor() + { + // Arrange + var accessor = new ActionContextAccessor(); + var context = new ActionContext(); + var domain = AppDomain.CreateDomain("newDomain"); + + // Act + domain.DoCallBack(DomainFunc); + AppDomain.Unload(domain); + accessor.ActionContext = context; + + // Assert + Assert.True(ReferenceEquals(context, accessor.ActionContext)); + } + } +#endif +}