Update application's libraries (DLLs) during runtime?

浪子不回头ぞ 提交于 2020-01-01 06:10:28

问题


Is there a way to update DLL while application is running in C#? For example, if there is DLL with a function that looks like this:

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

And it is called in a thread with 1 second sleep between calls.

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? 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. Am I wrong?


回答1:


If your code can be logically isolated into seperate DLLs, you can use (as others have mentioned) remoting or the System.AddIn framework. 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.

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. 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!




回答2:


You cannot update a dll while it is in use, however you can.

  • Make a copy of the dll with a new file name
  • Load the copy of the dll into an app domain
  • Talk to that app domain with .net remoting
  • When the original dll changes repeat the above

This is what Asp.net does under the covers. So another option is to use Aps.net to host your app.



来源:https://stackoverflow.com/questions/3803698/update-applications-libraries-dlls-during-runtime

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