Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
601 views
in Technique[技术] by (71.8m points)

c# - Unloading a dll file in mef

I have some plugins as dll files. My application loads the dll and it runs fine. but when I try to delete the old plugin and replace it with a new plugin it doesn't allow to do me. as it has been loaded by application. I have found that by using appdomain we can do that. but I am not able to find a solution in which mef is used.

I need a code which can run on mef. Below is my code which is used to load plugins.

//Creating an instance of aggregate catalog. It aggregates other catalogs
var aggregateCatalog = new AggregateCatalog();

//Build the directory path where the parts will be available
var directoryPath = "Path to plugins folder";

//Load parts from the available dlls in the specified path using the directory catalog
var directoryCatalog = new DirectoryCatalog(directoryPath, "*.dll");

//Add to the aggregate catalog
aggregateCatalog.Catalogs.Add(directoryCatalog);

//Crete the composition container
var container = new CompositionContainer(aggregateCatalog);


// Composable parts are created here i.e. the Import and Export components assembles here
container.ComposeParts(this);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i have found that by using appdomain we can do that. but i am not able to find a solution in which mef is used.

Unfortunately, this is not supported by MEF. MEF was designed specifically for application extensibility, not as a general purpose plugin system which supports unloading and replacement of code at runtime.

The only way to make this work would be to use MEF within a separate AppDomain, and unload the AppDomain as a whole. The CLR itself has no support for unloading a loaded assembly other than unloading the entire AppDomain in which the assembly is opened.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...