I\'m using MEF to load plugins in my app. Everything works, but I want new parts to be discovered when they are dropped into my app folder. Is this possible? DirectoryCatalog ha
You can use the FileSystemWatcher to detect new DLLs being dropped in your plugin folder. Then you can handle such events by calling DirectoryCatalog.Refresh or AggregateCatalog.Catalogs.Add to update the MEF composition with the new parts.
Some things to be aware of:
You need to mark your MEF imports as being designed to deal with recomposition, as explained in the MEF programming guide section on Recomposition. Otherwise MEF will raise an error when you try to update them.
FileSystemWatcher
raises events on system thread pool threads (unless you make use of the SynchronizingObject property). Be aware that if you call DirectoryCatalog.Refresh
from another thread, you must construct the CompositionContainer
with the isThreadSafeFlag
enabled. You'll also have to think about the thread safety of your property setters that will be called when the composition is updated.
You can also remove catalogs by taking them out of an AggregateCatalog.Catalogs
collection. But there is no way to unload the associated assemblies (except by unloading the entire Appdomain
). That also means you still can't delete or overwrite an assembly while the application is running.