动态加载.dll文件后释放.dll和.pdb [英] Release .dll and .pdb after loading .dll file dynamically

查看:281
本文介绍了动态加载.dll文件后释放.dll和.pdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Windows应用程序,通过单击一个按钮,可以在其中调用编码的UI项目(名为RecordAndPlayback的项目).我的目的是在编码的UI项目中调用测试方法,然后能够修改测试,然后从我的win应用中调用它,而无需使用相同的按钮将其关闭.

I'm building a windows application where I can call coded UI project (project named RecordAndPlayback) by clicking on a button. My aim is to call the test methods in the coded UI project and then be able to modify the test then call it back from my win app without closing it using the same button.

我设法调用了测试方法,但是在此之后修改测试方法时遇到了问题.每当我构建编码的UI项目时,都会出现错误

I managed to call the test method, but I have a problem in modifying the test method after that. Whenever I build my coded UI project, I get an error

错误CSC(0,0):创建调试信息文件'... \ obj \ Debug \ RecordAndPlayback.PDB'时发生意外错误-'... \ obj \ Debug \ RecordAndPlayback.pdb:进程无法访问文件,因为它正在被另一个进程使用.

ERROR CSC(0,0): Unexpected error creating debug information file '...\obj\Debug\RecordAndPlayback.PDB' -- '...\obj\Debug\RecordAndPlayback.pdb: The process cannot access the file because it is being used by another process.

我用AppDomain加载了其中的dll,然后卸载了AppDomain,希望该dll被释放.我设法做到了,但是随后在.pdb文件中出现了以上错误.我找到了另一个解决方案,我发现它更简单,但仍然遇到上述问题.

I used AppDomain to load the dll in it and then unload the AppDomain in hope that the dll will be released. I managed to do that, but then I got the above error in the .pdb file. I found another solution which I found much simpler but still got the above problem.

到目前为止,这是我的代码示例:

Here is my code sample so far:

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项目.

I tried using the AppDomain but it didn't work. Actually the above code is the best I got so far, I'm sure that the .dll file is released as I'm able to delete it. Also, the .pdb file in the bin folder is released (also was able to delete it). My problem is the .pdb file in the obj folder. The only way is that I must close my windows application project so I can build my coded UI project.

如何在不关闭我的Win应用程序的情况下解决此问题?

How can I fix this without closing my win app?

推荐答案

我找到了解决问题的方法.我尝试了许多在线发布的解决方案,但它们都存在相同的问题. .dll文件正在由另一个进程使用,或者.pdb文件正在由另一个进程使用.

I found a solution to my problem. I tried a lot of solutions posted online, and they all had the same problem. Either the .dll file is being used by another process, or the .pdb file is being used by another process.

我上面提到的代码可以正常工作,但.pdb文件除外,老实说,我没有找到解决方案.因此,我解决了这个问题.我已经修改了build选项,因此根本不产生.pdb文件.

The code I have above, as I mentioned is working except for the .pdb file, which honestly I didn't find a solution to it. So, I worked around it. I have modified the build option so I don't produce the .pdb file at all.

要禁用pdb文件的生成,需要在单击构建"选项卡下部的""Advanced..."按钮"之后,使用项目属性中可用的"Advanced build settings"对话框.

In order to disable pdb file generation, you need to use the "Advanced build settings" dialog available in project properties after clicking the "Advanced..." button" located in the lower part of the Build tab.

Output - Debug info: to None设置为发行版本配置,将不会生成pdb文件.

Set Output - Debug info: to None for release build configuration and no pdb files will be generated.

解决了我的问题.尽管它并没有真正解决问题,但是我删除了导致问题的文件.希望这对其他人有帮助

This fixed my problem. Although it didn't really fix it, but I removed the file causing the problem. Hope this helps other

这篇关于动态加载.dll文件后释放.dll和.pdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆