Change Dll loaded with MEF
Asked Answered
mef
G

2

4

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

Girondist answered 4/1, 2011 at 15:28 Comment(0)
B
4

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...

Bankrupt answered 4/1, 2011 at 15:50 Comment(1)
I've tryed many way, but unable to make it working. I had to make my application restard to have the opportunity to update dll :(Girondist
C
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...

Canal answered 13/2, 2013 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.