AppDomain.DynamicDirectory 未生成

AppDomain.DynamicDirectory is not generated

我正在使用以下代码创建一个 AppDomain

String pa = @"C:\Users\user\AppData\Local\Temp\db5fjamk.xnl";
System.IO.Directory.CreateDirectory(pa);
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; //f:\projectpath\out\debug-i386-unittest\UnitTests
setup.ApplicationName = string.Concat(AppDomain.CurrentDomain.FriendlyName, DateTime.UtcNow.Ticks); //UnitTestAdapter: Running test636559691791186101
setup.DynamicBase = pa;
Evidence evidence = AppDomain.CurrentDomain.Evidence;
_Domain = AppDomain.CreateDomain(setup.ApplicationName, evidence, setup);

但是_Domain.DynamicDirectory属性不存在。 https://msdn.microsoft.com/en-us/library/system.appdomain.dynamicdirectory(v=vs.110).aspx 明确表示使用了 AppDomainSetup.DynamicBase。

在 vstest.console.exe 中执行会改变 App Domains 行为的原因可能是什么。有解决办法吗。

解决方案

检查 AppDomain.CurrentDomain.FriendlyName 是否包含冒号 (:) 等非法字符。如果是,您应该使用 SO 问题 How to remove illegal characters from path and filenames?.

中讨论的方法之一对 setup.ApplicationName 进行清理

背景

当我调试测试时,我得到了 System.NotSupportedException 消息 The given path's format is not supported.

堆栈跟踪是

at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
at System.AppDomain.get_DynamicDirectory()
at System.AppDomain.get_DynamicDirectory()
at SO_AppDomain.Sut.Method() in <path>\Program.cs:line 30
at UnitTestProject1.UnitTest1.TestMethod1() in <path>\UnitTest1.cs:line 14

AppDomain.CurrentDomain.FriendlyName 的值为 TestSourceHost: Enumering assembly

快速浏览 reference source of EmulateFileIOPermissionChecks - which is the last method that appears in the stack trace - revealed that it throws a NotSupportedException if PathInternal.HasInvalidVolumeSeparator returns 正确。该方法包含以下注释:

 // Toss out paths with colons that aren't a valid drive specifier.
 // Cannot start with a colon and can only be of the form "C:" or "\?\C:".

字符串 TestSourceHost: Enumering assembly 显然违反了该规则。