应用程序使用的DLL热卸载/重装 [英] Hot Unload/Reload of a DLL used by an Application

查看:105
本文介绍了应用程序使用的DLL热卸载/重装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有加载DLL执行处理的特定部分应用

I have an application that loads a DLL to execute a specific part of processing

Example : "Application.dll" loading "Process.dll" 

Process.dll动态加载在运行,使用反射,而不是在应用程序中引用。

Process.dll is dynamically loaded at Runtime, using reflection, and not referenced in the application.

处理结束后,将DLL需要在服务器上进行重新编译和稍后重新装回。
结果为了做到这一点,我需要释放,否则我得到以下信息:
无法复制文件Process.dll到Process.dll该进程无法因为它正被另一个进程访问文件Process.dll'。

After processing is finished, the DLL needs to be recompiled on server and loaded back again later.
In order to do so, I need to free it, otherwise I get the following message : "Unable to copy file "Process.dll" to "Process.dll". The process cannot access the file 'Process.dll' because it is being used by another process."

所以,问题是:如何以编程方式免费/发行/再次装入之前,从我的应用程序卸载 Process.dll 。当然,整点就是做这个的停止应用程序。

So the question is : How to programmatically free/release/unload the Process.dll from my application before loading it back again. Of course,the whole point is to do this WITHOUT stopping the Application.

编辑1:

一个提出的解决方案是这样的:

A proposed solution goes like this :

AppDomain newDomain4Process = AppDomain.CreateDomain("newDomain4Process");
Assembly processLibrary = newDomain4Process.Load("Process.dll");
AppDomain.Unload(newDomain4Process);

我仍然有问题是,虽然我给正确的完整路径,我收到了 FileNotFound异常。这个问题的答案<一个href=\"http://stackoverflow.com/questions/2132649/loading-unloading-assembly-in-different-appdomain\">this帖子没有预期的效果无论是。

The problem I am still having is that, though I am giving the proper full path, I get a FileNotFound Exception. The answer to this post did not have the expected effect either.

编辑2:

<一个href=\"http://stackoverflow.com/questions/658498/how-to-load-assembly-to-appdomain-with-all-references-recursively\">This帖子救了我的生活,这里是code:

This post saved my life, here is the code :

class ProxyDomain : MarshalByRefObject
    {
        public Assembly GetAssembly(string AssemblyPath)
        {
            try
            {
                return Assembly.LoadFrom(AssemblyPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

   ProxyDomain pd = new ProxyDomain();
   Assembly a = pd.GetAssembly(FullDLLPath);

修改3:结果
我没有访问的AppDomain并与previous的解决方案,虽然其卸载。
当我使用的AppDomain创作的经典方法,我觉得到阿列克谢的警告: AppDomain.Unload 似乎工作,但大会仍然加载(模块视图)。
所以,我仍然有我的问题,在某种程度上,因为我真的不能有效卸载DLL。

EDIT 3 :
I didn't get access to the AppDomain and unload it with the previous solution though. When I used the classic method of AppDomain Creation, I felt into Alexei's warning : AppDomain.Unload "seemed" to work, but the assembly was still loaded (Module View). So I still have my problem in some way, since I can't really unload the DLL efficiently.

推荐答案

它已经相当一段时间,因为我看着这一点,但我相当肯定,你需要创建一个新的AppDomain 然后加载里面的DLL。

It's been quite a while since I looked at this but I'm fairly sure that you'd need to create a new AppDomain and then load the DLL inside there.

原因是,你不能卸载的大会由它的自我,但你可以卸载一个的AppDomain 的包含大会

The reason is that you can't unload an Assembly by it self but you can unload an AppDomain that contains an Assembly.

下面是描述如何做到这一点$ C $的CProject文章:要卸载,运行DLL

Here's a CodeProject article that describes how to do this: To Unload a Running Dll

这篇关于应用程序使用的DLL热卸载/重装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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