Unloading Addin

Sep 10, 2008 at 10:13 PM
Hi,

I'm using the DirectoryPartCatalog and i'm happy with the approach (I don't have to reinvent the wheel), however is it possible to unload an addin?

I want to swap in extensions (Logging maybe?) while the application is executing in order for it to have different behaviour.  At the moment, the dlls are locked.

Maybe the approach is to load both addins and have a way to select between them, could be difficult if its running as a background windows service :(

Thanks

Ben
Sep 11, 2008 at 12:32 AM

You are right, Ben. The dlls are locked and cannot be deleted.

However, you can move those dlls out of the watching directory of the DirectoryPartCatalog. This will trigger a re-composion of the container and those parts that are contained in the moved assemblies will be removed from the container. Because of a known bug, this does not work in current drop of MEF yet. But I am sure you should be able to try this out in our new drops.

As you mentioned above, loading all addins and selecting the one you want to use is actaully a good way to go, which we have discussed in another thread.

Thanks for your feedback!

Zhenlan

May 5, 2009 at 10:44 AM
This is a feature I would really like to see. Has it been done yet? Can I just click on a button in my application and then the part will close and disappear.
May 12, 2009 at 8:08 PM
Edited May 12, 2009 at 9:17 PM

I am also interested in this feature to implement an update mechanism of my plugins (I.e.: download new versions and replace current ones).

I could do the update before starting the application but I actually intend to use the plugins in a windows service which will run on servers. This service will therefore not be restarted often and I think I will have to unload the plugins once I detect that an update is available.

In the meantime, could you tell me if using the Fusion's shadow copy feature will do the trick ? (http://blogs.msdn.com/junfeng/archive/2004/02/09/69919.aspx). I mean, will a new version of a plugin saved in the watching directory be taken into account by the DirectoryPartCatalog if this feature is enabled to avoid the locks ?

O.

Developer
May 14, 2009 at 12:10 AM

You can certainly use the shadow copy trick to get around the locking of the assemblies, however that will not give you the ability to unload an already loaded assembly from the application. Currently the CLR does not support unloading individual assemblies. The only way you can ever unload something from the running process is via a custom AppDomain. AppDomains can be loaded and unloaded in a running process and with them any assemblies that are loaded into them. However there is a boundary you need to cross for code to talk to each other across AppDomains. This boundary requires you to marshal calls in and out, almost like if you were marshalling across process boundaries.

Unfortunately Isolation is a hard problem to solve, MAF (Managed Addin Framework) tried to do some of this isolation work for you so if you are interested you might want to look into it.