【发布时间】:2018-11-09 21:46:45
【问题描述】:
我正在构建一个 Windows 应用程序,我可以通过单击按钮调用编码的 UI 项目(名为 RecordAndPlayback 的项目)。我的目标是在编码的 UI 项目中调用测试方法,然后能够修改测试,然后从我的 win 应用程序中调用它,而无需使用相同的按钮关闭它。
我设法调用了测试方法,但是在那之后我在修改测试方法时遇到了问题。每当我构建编码的 UI 项目时,都会出现错误
ERROR CSC(0,0): Unexpected error creating debug information file '...\obj\Debug\RecordAndPlayback.PDB' -- '...\obj\Debug\RecordAndPlayback.pdb: 进程无法访问文件,因为它正被另一个进程使用。
我用AppDomain加载了里面的dll,然后卸载了AppDomain,希望dll可以被释放。我设法做到了,但后来我在 .pdb 文件中得到了上述错误。我找到了另一个解决方案,我发现它更简单,但仍然遇到上述问题。
到目前为止,这是我的代码示例:
string dllPath = @"...\bin\Debug\RecordAndPlayback.dll";
byte[] fileContent;
using (FileStream dll = File.OpenRead(dllPath))
{
fileContent = new byte[dll.Length];
dll.Read(fileContent, 0, (int)dll.Length);
}
var DLL = Assembly.Load(fileContent);
Playback.Initialize();
foreach (Type type in DLL.GetExportedTypes())
{
if (type.Name == "CodedUITest1")
{
var c = Activator.CreateInstance(type);
try
{
type.InvokeMember("CodedUITestMethod1", BindingFlags.InvokeMethod, null, c, new object[] { });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
break;
}
}
Playback.Cleanup();
我尝试使用AppDomain,但没有成功。实际上上面的代码是迄今为止我得到的最好的代码,我确信 .dll 文件已被释放,因为我可以删除它。此外,bin 文件夹中的 .pdb 文件被释放(也可以删除它)。我的问题是 obj 文件夹中的 .pdb 文件。唯一的方法是我必须关闭我的 Windows 应用程序项目,以便我可以构建我的编码 UI 项目。
如何在不关闭我的 win 应用程序的情况下解决此问题?
【问题讨论】:
-
你的代码在哪里使用
AppDomain?那将是正确的做法。也许你做错了什么。 -
我使用了解决方案here,并将AppDomain代码放在了DoWork()方法中。我放置的代码是我在问题中的代码。
标签: c# assemblies coded-ui-tests dynamic-assemblies