问题
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
- 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: - 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