简体   繁体   English

.NET dll热插拔,没有应用程序重启

[英].NET dll hot swap, no application restart

Suppose that you have the following situation in .NET (C#): 假设您在.NET(C#)中有以下情况:

namespace MyDll
{
    public class MyClass
    {
        public string GetValue()
        {
            return "wrong value";
        }
    }
}

this code goes compiled into a dll say, MyDll.Dll. 这段代码编译成一个dll说,MyDll.Dll。

Then you have an application called MyApplication.exe that, using MyDll.dll as reference, creates an instance of the class MyClass and calls the method GetValue: 然后你有一个名为MyApplication.exe的应用程序,使用MyDll.dll作为参考,创建一个MyClass类的实例并调用方法GetValue:

MyClass instance = new MyClass();
instance.GetValue();

Once you realize that the current implementation of MyClass.GetValue() is wrong is there any way to fix the method MyClass.GetValue() like this 一旦你意识到MyClass.GetValue()的当前实现是错误的,有什么方法可以修复MyClass.GetValue()这样的方法

namespace MyDll
{
    public class MyClass
    {
        public string GetValue()
        {
            return "correct value";
        }
    }
}

and HOT swapping the resulting MyDll.dll, without restarting MyApplication.exe??? 和HOT交换生成的MyDll.dll,而不重新启动MyApplication.exe ???

All solutions proposed in stackoverflow and in google fail to work because, even if MyDll.dll is loaded on a new AppDomain created for that purpose, when I unload calling stackoverflow和google中提出的所有解决方案都无法正常工作,因为即使MyDll.dll加载到为此目的创建的新AppDomain上,当我卸载调用时也是如此

AppDomain.Unload(anoterAppDomainJustForMyDll);

it returns without error, but if I try to overwrite the original MyDll.dll with the corrected one (while MyApplication.exe is still running) I get an error "impossible to overwrite because the dll in use by another process".... 它返回没有错误,但如果我尝试用更正的一个覆盖原始的MyDll.dll(当MyApplication.exe仍在运行时)我收到错误“无法覆盖,因为另一个进程使用的dll”....

Question is closed by myself: please refer to article in codeplex 问题由我自己关闭:请参阅codeplex中的文章

To me it was important to be able to hot swap the new dll without rebooting the application, and as the article proposes, this can be done, as this is done from withing the application itself. 对我而言,能够在不重新启动应用程序的情况下热交换新的dll非常重要,正如文章所提出的,这可以完成,因为这是通过使用应用程序本身来完成的。 The reason why my previous attemps were failing was that I tried to overwrite the target dll from outside (in explorer in this case). 我之前的尝试失败的原因是我试图从外部覆盖目标dll(在这种情况下是在资源管理器中)。 But if the overwriting is done like in the proposed solution, from the application itself, it works as expected. 但是如果覆盖是在提议的解决方案中完成的,那么从应用程序本身来看,它可以按预期工作。

I will need a little bit more on application side to define directories where versionable ddls can be deployed, but this is perfectly acceptable to me. 我需要在应用程序端更多地定义可以部署可版本化ddls的目录,但这对我来说是完全可以接受的。

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

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