AppDomain safety

This commit is contained in:
BrennanConroy 2016-07-26 13:01:49 -07:00
parent 855974f128
commit 0a5f8e013e
2 changed files with 39 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;
#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
{

View File

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