Search code examples
dllmefdynamicreload

Dynamically change parts (DLLs) when using a DirectoryCatalog


Using MEF, after adding new plugins (DLLs containing parts) to plugins folder, calling a refresh on DirectoryCatalog would update container (if recomposition is allowed) and new added plugins become accessible.

My question is what if we need to replace a DLL (part) dynamically ?

I tried this but all loaded parts are locked (write-protected) by MEF and cannot be replaced.


Solution

  • It's .NET that has a lock on your DLLs. You can't unload assemblies from an AppDomain, and while they are loaded there will normally be a lock. You can enable shadow copying for the AppDomain to let you delete the DLLs. They would still be loaded but the DirectoryCatalog would see they were gone when you called Refresh and remove them from the catalog.