Search code examples
c#.netpluginsmodulemef

MEF Plugins Loading and Unloading During Program Execution


We have an application which has functionality which can be implemented in several different ways. We have a standardized interface (say, IWorker) that models the functionality required. We have several modules, each of which has its own implementation of IWorker, that handles the necessary tasks in vastly different ways. Currently, depending on which version of the application is necessary, we supply the specific DLL containing the appropriate module, and everything is fine. However, we now wish to support the scenario wherein the user can change out modules at runtime based on changing conditions (i.e., network connection is lost, so we want to now use the IFileWorker module instead of the INetworkWorker module). How can we unload a module and load in a different one at runtime?


Solution

  • instead of load and unload your modules, you could go a simple way and create a IWorkerController class (export shared) wich has a collection of all IWorker modules loaded via MEF. so if you want to use the IWorker functionality - you just ask your IWorkerController (maybe default is INetworkWorker). you can simply switch to the iworker you want by telling your IWorkerControll to do so.

    so instead of taking IWorker to all your services you now take IWorkerController.