0

I'm having issues with MEF where I have a DirectoryCatalog and in a later stage want to overwrite the assembly and "refresh" the catalog.

The problem i'm running into is that the file simply is "in use" and I can't overwrite the file. Normaly you are able to overwrite a .Net assembly.

I quess MEF has it in use, but how does this match with Recompilation?!

Here is my code example. Even with local variables the file is still in use.

I've also tried to have the assembly in both application and plugins folder but then the app folder version is used and therefor overwriting does not make a difference.

public RecompilationExample()
{
    DirectoryInfo dir = new DirectoryInfo(".\\plugin");

    if (!dir.Exists)
        dir.Create();

    DirectoryCatalog d; 
    CompositionContainer c;

    d = new DirectoryCatalog(".\\plugin");
    d.Changed += new EventHandler<ComposablePartCatalogChangeEventArgs>(d_Changed);

    c = new CompositionContainer(d);
    c.ExportsChanged += new EventHandler<ExportsChangeEventArgs>(c_ExportsChanged);
    c.ComposeParts(this);
}

1 Answers1

3

Normaly you are able to overwrite a .Net assembly.

As far as I know, no. A loaded .NET assembly cannot be overwritten. You also can't unload a loaded assembly (except by unloading the entire AppDomain it is hosted in).

What you can do instead is to use shadow copying, i.e. copying the assembly and then loading the copy. You can enable this with the AppDomainSetup.ShadowCopyFiles property. This is typically used in ASP.NET and allows you to overwrite the original file, but not in a way that influences the running process - until you restart it.

See also this other answer I wrote in response to a similar question. Long story short: You can use DirectoryCatalog.Refresh to add new assemblies on the fly, but not to replace or remove them. When you need to replace assemblies, the best solution is to restart your process.

Community
  • 1
  • 1
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • okee, I get that. What i ment with "Normaly you are able to overwrite a .Net assembly" are actually the assemblies in the BIN folder which in turn invokes a restart of the app ... probably using the shadow copy logic. (Apparantly is posting the comment) What i don't get is MEF actually locking the file and not working the same as the other assemblies in the BIN folder. Wouldn't this also mean that i'm not even able to uninstall the application without stopping the app, because the file is in use and can't be deleted. – martijnvanschie Oct 24 '11 at 11:15
  • @martijnvanschie: Perhaps those other binaries are shadow-copy enabled in some other way that doesn't affect the MEF assemblies. Have you tried putting `AppDomainSetup.ShadowCopyFiles = "true";` before the code which sets up the MEF container? – Wim Coenen Oct 24 '11 at 13:36