Replace assembly at runtime with .NET
Asked Answered
O

5

12

Is there a way with a plugin system (I would use with an IoC container) to load one version of an assembly at runtime and then replace that DLL while the AppDomain is running? I don't want to restart the application.

Does MEF do something like this?

Olmos answered 22/2, 2011 at 16:25 Comment(0)
S
4

This is essentially what NUnit does (or at least did, I haven't been in the code in a while). But it does it by loading the test assembly in another AppDomain, calling code in that domain using the DoCallback method of AppDomain and then reloads the test assembly if it is recompiled.

So while you can't unload or reload a dll, but you can unload and reload an appdomain and execute code in it.

Salvador answered 22/2, 2011 at 16:39 Comment(0)
Z
1

It is impossible using pure .net, because there is no way to unload assembly from domain. Since MEF is written in managed code I doubt that it is possible. I solved this issue by loading assembly to separate domain and when I wanted to reload it I stoped it and started again.

Zajac answered 22/2, 2011 at 16:31 Comment(3)
Can one appdomain access other code in other appdomains? Suppose I have it registered in my IoC container, could it resolve it?Olmos
@Daniel yes, but you need special code to access the code in the other appdomain.Oxbridge
If I'm not mistaken, MEF can be configured to load plugins in separate AppDomains.Quaver
M
1

http://msdn.microsoft.com/en-us/library/ms173101(v=VS.90).aspx

http://people.oregonstate.edu/~reeset/blog/archives/466

Magistracy answered 22/2, 2011 at 16:43 Comment(4)
I don't need it at design time.Olmos
I don't see how programming against interfaces helps in this case. I'm all for it, but if the dll needs to be reloaded for whatever reason you still have to handle that. I'm not sure what the reason is, but if it is a real reason than programming against interfaces isn't enough I think.Salvador
so you want to reload an assembly because it has changed? Like doing an update without any downtime?Magistracy
How were you thinking of notifying the app that it has changed? Polling the directory, a SQL record, or what? I would suggest loading the assembly on startup as a plugin using Assembly.Load (or whatever it is) and once there is a new dll, just dispose of that assembly and load up the new one. You can create a singleton to do this. MyPlugin.Get() will do all the work in the background, either send the latest ver or dispose old one, get new one and return that.Magistracy
A
0

It looks like this CodeProject article explains how to do it. This question on the MSDN Forums seems similiar and this SO question shows how to do it. All of these links warn of leaks being created by problems disposing AppDomains properly, so buyer beware.

Agneta answered 22/2, 2011 at 23:45 Comment(1)
I'm interested if anyone has any experience doing this, or thoughts leaning toward using another mechanism to be able to update a plugin-enabled MVC website on the fly.Agneta
C
0

You cannot unload dll in a running app domain. What you can do is use MEF and prepare your app to handle multiple implementations. In that case, you can copy a new dll (a new implementaion of an interface, module, etc.) into the MEF folder, recompose and use it. But, careful, it is gonna cost you memory.

You can read about it and download sample here.

Confirmatory answered 5/12, 2013 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.