Update application's libraries (DLLs) during r

2020-06-22 08:23发布

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?

标签: c# dll
2条回答
你好瞎i
2楼-- · 2020-06-22 09:18

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.

查看更多
等我变得足够好
3楼-- · 2020-06-22 09:26

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!

查看更多
登录 后发表回答