HamedFathi
HamedFathi

Reputation: 3979

Why is an AppDomain relevant to a plugin manager implemented with MEF

I wrote a simple plugin manager class with MEF and FileSystemWatcher for refreshing plugins automatically, but I hear some say that to be able to add and remove assemblies (plugins) on the fly we need to use an AppDomain also.

Can anyone guide me as to when we need to use AppDomain and MEF together (especially for my plugin manager scenario)?

What is the relationship between them ?

Upvotes: 0

Views: 295

Answers (1)

strongbutgood
strongbutgood

Reputation: 675

In short, an AppDomain is required when you want to overwrite .dlls that are in use by an application, and that AppDomain must specify the ShadowCopyFiles = "true".

Shadow copying files means that the AppDomain will copy the .dlls to a temporary directory and load them from that temporary directory so the original .dlls can be overwritten.

Unfortunately the assemblies which are loaded into any AppDomain cannot be unloaded unless the AppDomain that contains them is unloaded.

With that in mind, refreshing a plugin is difficult because you would have to A) unload the entire AppDomain which necessarily unloads all other .dlls in that AppDomain, or B) allow a new version of the same .dll to be loaded increasing the memory footprint of your application. The second option also requires your plugin .dlls to be strong named and a different version number in order for MEF to recognise a difference and load the new .dll.

Upvotes: 1

Related Questions