Change Dll loaded with MEF

荒凉一梦 提交于 2020-01-01 05:47:08

问题


I'm using MEF and the System.ComponentModel.Composition.dll to load some dll.

I'm doing something like :

AggregateCatalog catalog = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog(directory));
_container = new CompositionContainer(catalog);
_container.ComposeParts(this);

to import my dll.

After some times, I would like to update my dll but if I try to delete it, I have an access denied, because it's alrealdy used by the program.

How can I release the dll, replace with a new dll and load the dll again ? (without closing the program)

Thanks in advance for your help


回答1:


You need to enable shadow copying in the AppDomain, this forces the application to behave similarly to a web app where the executable content is not running from the source location, but a temporary location.

The only problem you have is either

  1. Use an obselete method AppDomain.CurrentDomain.SetShadowCopyFiles() which forces it on the the current domain. Not advisable as this has been deprecated in favour of:
  2. Use AppDomainSetup.ShadowCopyFiles = "true"; when creating a new AppDomain. You then need to defer execution of the your assembly in the other AppDomain. Maybe this form post can help?

I'm not sure you can enable Shadow Copying through application configuration...




回答2:


If you try to insert on catalog one Object Assembly like this:

Assembly assembly = Assembly.Load(System.IO.File.ReadAllBytes(Path.Combine(directoryPath, ItemPlugin)));
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(assembly));

You can Delete\Change the file later...



来源:https://stackoverflow.com/questions/4595304/change-dll-loaded-with-mef

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!