MEF and Shadow Copy Files Sample?

Nov 2, 2009 at 9:51 PM

Hi, there's been some discussions around how to be able to update assemblies loaded in a DirectoryCatalog by using shadow copy. For someone who hasn't been into app domains and shadow copy before, a small sample would be of much help. I've scanned around for a sample without much success, and before I sit down and make a fool of myself with app domains I hope there is a sample of MEF + shadow copy out there!

Thanks for any help,

Johan

Developer
Nov 2, 2009 at 11:10 PM

I don't know of any samples that currently do this. However I did a quick experiment and put together some code to get you started:

public interface IEntryPoint
{
    void Run();
}

public class Program
{
    private static void Main(string[] args)
    {
        if (AppDomain.CurrentDomain.IsDefaultAppDomain())
        {
            var setup = new AppDomainSetup();
            setup.ShadowCopyFiles = "true";
            var appDomain = AppDomain.CreateDomain("Host_AppDomain", AppDomain.CurrentDomain.Evidence, setup);
            appDomain.ExecuteAssembly(Assembly.GetExecutingAssembly().CodeBase);
        }
        else
        {
            // This work will happen in the shadow copied AppDomain.

            // All the types referenced in this Main method will be loaded in the default 
            // AppDomain and thus cause there containing assembly to be locked so be careful
            // not to reference any types outside the framework or this assembly (which will have to be locked).

            var catalog = new DirectoryCatalog("Extensions");
            var container = new CompositionContainer(catalog);

            var entryPoint = container.GetExportedValue<IEntryPoint>();
            entryPoint.Run();
        }
    }
}

Developer
Nov 2, 2009 at 11:26 PM

Keep in mind I'm no expert in AppDomains I'm just doing some experimenting. And after a little more experimenting I think it is better to use appDomain.DoCallback instead of appDomain.ExecuteAssembly like such:

public class Program
{
    private static void Main(string[] args)
    {
        var setup = new AppDomainSetup();
        setup.ShadowCopyFiles = "true";
        var appDomain = AppDomain.CreateDomain("Host_AppDomain", AppDomain.CurrentDomain.Evidence, setup);
        
        appDomain.DoCallBack(new CrossAppDomainDelegate(DoWorkInShadowCopiedDomain));            
    }

    private static void DoWorkInShadowCopiedDomain()
    {
        // This work will happen in the shadow copied AppDomain.

        // All the types referenced in this  method will be loaded in the default 
        // AppDomain and thus cause there containing assembly to be locked so be careful
        // not to reference any types outside the framework or this assembly (which will have to be locked).

        var catalog = new DirectoryCatalog("Extensions");
        var container = new CompositionContainer(catalog);

        var entryPoint = container.GetExportedValue<IEntryPoint>();
        entryPoint.Run();
    }
}
Aug 7, 2013 at 5:54 AM
Reviving this...

Here's an article I wrote in regards to MEF and AppDomains.

MEF and AppDomain - Remove Assemblies On The Fly

-John