Tim
Tim

Reputation: 1769

Change Dll loaded with MEF

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

Upvotes: 4

Views: 1633

Answers (2)

jcvpacheco
jcvpacheco

Reputation: 101

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

Upvotes: 2

Matthew Abbott
Matthew Abbott

Reputation: 61589

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

Upvotes: 4

Related Questions