How to add UNC path support to DirectoryCatalog

Aug 30, 2010 at 2:53 PM
Edited Aug 30, 2010 at 3:04 PM

MEF's DirectoryCatalog currently does not support UNC paths.  After searching the web for a workaround and discovering others asking the same question, I decided to take a closer look at MEF's source code itself.

Hope this helps.  Ideally, this feature will be added to a future "official" iteration of MEF, and we won't have to compile our own versions.  The code below is based on MEF 2 Preview 1.

 

[System\ComponentModel\Composition\Hosting] DirectoryCatalog.cs:

    private static string GetFullPath(string path)
    {
        if (!IOPath.IsPathRooted(path)
            && path.StartsWith(@"\\") == false   // <-- new code
            && AppDomain.CurrentDomain.BaseDirectory != null)
        {
            path = IOPath.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
        }

        return IOPath.GetFullPath(path).ToUpperInvariant();
    }

[System\ComponentModel\Composition\Hosting] AssemblyCatalog.cs:

    private static Assembly LoadAssembly(string codeBase)
    {
        Requires.NotNullOrEmpty(codeBase, "codeBase");

        AssemblyName assemblyName;

        try
        {
            assemblyName = AssemblyName.GetAssemblyName(codeBase);
        }
        catch (ArgumentException)
        {
            assemblyName = new AssemblyName();
            assemblyName.CodeBase = codeBase;
        }

        // new code - seems to be a bug in AssemblyName.GetAssemblyName() or Assembly.Load()
        if (assemblyName.CodeBase.StartsWith(@"file:////"))
        {
            assemblyName.CodeBase = assemblyName.CodeBase.Replace(@"file:////", @"file://");
        }

        return Assembly.Load(assemblyName);            
    }

 

Sep 8, 2010 at 5:33 PM

If any changes are to be made they should probably go into the IOPath.IsPathRooted method, so that it can identifiy all UNC paths when they are rooted.

The second method is for loading assemblies by name, not file. I don't see why you would expect an URI to be passed to that method.

Are you actually loading assemblies from a network share?