简体   繁体   English

在运行时更新应用程序的库(DLL)?

[英]Update application's libraries (DLLs) during runtime?

Is there a way to update DLL while application is running in C#? 有没有办法在C#中运行应用程序时更新DLL? For example, if there is DLL with a function that looks like this: 例如,如果存在具有如下函数的DLL:

void write()
{
    Console.WriteLine("LALALA");
}

And it is called in a thread with 1 second sleep between calls. 它在一个线程中调用,在调用之间有1秒的休眠时间。

Now, I wrote the new version: 现在,我写了新版本:

void write()
{
    Console.WriteLine("LA LA LA\n");
}

Can I update old DLL with this new one during runtime? 我可以在运行时使用这个新DLL更新旧的DLL吗? It is important that my APP is up and running all the time, no meter what... but I guess that updating libraries is an exception. 重要的是我的APP一直在运行,没有什么......但我想更新库是个例外。 Am I wrong? 我错了吗?

You cannot update a dll while it is in use, however you can. 您无法在使用时更新dll,但是可以。

  • Make a copy of the dll with a new file name 使用新文件名制作dll的副本
  • Load the copy of the dll into an app domain 将dll的副本加载到应用程序域中
  • Talk to that app domain with .net remoting 使用.net远程处理与该应用程序域对话
  • When the original dll changes repeat the above 当原来的dll改变重复以上

This is what Asp.net does under the covers. 这就是Asp.net所做的事情。 So another option is to use Aps.net to host your app. 所以另一个选择是使用Aps.net来托管你的应用程序。

If your code can be logically isolated into seperate DLLs, you can use (as others have mentioned) remoting or the System.AddIn framework. 如果您的代码可以在逻辑上隔离成单独的DLL,您可以使用(如其他人所提到的)远程处理或System.AddIn框架。 Depending on how integrated the functionality is though, it may be far too complicated and/or overkill. 根据功能的集成程度,可能过于复杂和/或过度杀伤。 One benefit of the System.AddIn method is that assemblies can be unloaded, the dll replaced with a new version, and reloaded again without stopping the app - it is only designed for lightweight plugin architectures though, and performance across the 'isolation boundary' isn't going to be as great. System.AddIn方法的一个好处是可以卸载程序集,用新版本替换dll,并在不停止应用程序的情况下重新加载 - 它仅用于轻量级插件体系结构,并且跨越“隔离边界”的性能不是不会那么好。

Scripting may also be useful if the areas are small enough (something like http://www.ironpython.net/ ) - you can store the code in text files (or database, or wherever) and load/run it from there. 如果区域足够小(如http://www.ironpython.net/ ),脚本也可能很有用 - 您可以将代码存储在文本文件(或数据库,或任何地方)中,然后从那里加载/运行它。 Then just replace the code and reload it. 然后只需替换代码并重新加载它。

I guess it all comes down to whether you can identify what is likely to change, and if there is an isolation process that would suit it. 我想这一切都取决于你是否可以确定可能发生的变化,以及是否存在适合它的隔离过程。 To do it to the entire application isn't easy! 要做到整个应用程序并不容易!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM