简体   繁体   中英

.NET dll hot swap, no application restart

Suppose that you have the following situation in .NET (C#):

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

this code goes compiled into a dll say, 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:

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

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

and HOT swapping the resulting MyDll.dll, without restarting 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

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

Question is closed by myself: please refer to article in 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. The reason why my previous attemps were failing was that I tried to overwrite the target dll from outside (in explorer in this case). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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