Release .dll and .pdb after loading .dll file dynamically

浪尽此生 提交于 2019-12-25 17:56:39

问题


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.

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

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.

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();

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.

How can I fix this without closing my win app?


回答1:


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.

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.

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.

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



来源:https://stackoverflow.com/questions/50614823/release-dll-and-pdb-after-loading-dll-file-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!