Question

I have a interface as follows:

[InheritedExport(typeof(ITransform))]
public interface ITransform
{...}

Now, I have two classes:

namespace ProjectA
{
    public class Transform:ITransform {....}
}

And

namespace ProjectB
{
    public class Transform:ITransform {....}
}

I am using DirectoryCatalog for loading each parts. Each project is compiled and their binaries(build output) location is given as an input to DirectoryCatalog for further composition.

The code for fetching ITransform parts is as follows:

public static class ExtensionFactory
    {        
        public static ITransform GetExtension(string extensionPath)
        {            
            IEnumerable<ITransform> extensions = null;          
            try
            {                
                AggregateCatalog catalog = new AggregateCatalog();
                catalog.Catalogs.Add(new DirectoryCatalog(extensionPath));      
                CompositionContainer container = new CompositionContainer(catalog);
                container.ComposeParts(catalog);
                extensions = container.GetExportedValues<ITransform>();
                return extensions.FirstOrDefault();
            }
            catch (Exception ex) {........}
            return extensions.FirstOrDefault(); 
        }        
    }

I have another project ProjectXYZ(auto generated by third party tool(Altova Mapforce 2012 SP1)).

For ProjectA:

namespace ProjectXYZ 
{
    public classA{...}
}

For ProjectB:

namespace ProjectXYZ 
{
    public classA{...}
    public classB{...}
}

ProjectA.Transform uses ProjectXYZ.ClassA, whereas ProjectB.Transform uses ProjectXYZ.ClassB from another implementation of ProjectXYZ. The implementation and classes of ProjectXYZ varies across for different implementation of ITransform. The classes in ProjectXYZ are automatically generated through some third-party tools, which I need to use directly. So, I cannot make any changes to ProjectXYZ.

So, when first time MEF loads ProjectA.Transform, it also loads ProjectXYZ to be used as a reference for ProjectA. When ProjectB.Transform is getting loaded/exported, then as ProjectXYZ being already in MEF memory, it uses the ProjectXYZ reference available from ProjectA. Thus, when ProjectB.Transform is executing, it searches for ProjectXYZ.ClassB, which it does not gets as MEF has load ProjectXYZ reference available in ProjectA.

How to resolve this problem. The MEF loads the parts correctly, but it does not load the supporting dll's references in a desired manner. I have also tried PartCreationPolicy attribute, but the results are same.

Was it helpful?

Solution

It not a MEF problem. The problem is in the loading model of .NET. (or better the way you're objects are loaded by .net)

When MEF loads it returns the correct objects. But when looking for class ProjectXYZ when projectB is loaded there is already a ProjectXYZ dll loaded with the correct assembly name projectB is referring to. And the loader the dll actually referenced by projectB is not loaded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top