Daniel A. White
Daniel A. White

Reputation: 190907

Replace assembly at runtime with .NET

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?

Upvotes: 12

Views: 4819

Answers (5)

Andras Sebo
Andras Sebo

Reputation: 1140

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.

Upvotes: 0

JoeB
JoeB

Reputation: 2815

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.

Upvotes: 0

Mike Two
Mike Two

Reputation: 46173

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.

Upvotes: 4

Andrey
Andrey

Reputation: 60065

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.

Upvotes: 1

Related Questions