Search code examples
c#.net-4.0mef

Unable to call a library from other in MEF


I have 3 class libraries, LibA, LibB & LibC. These libraries have defined classes A, B & C respectively.

class C
{
  public IEnumerable<X> FuncInC()
  {      
    return something;
  }
}

LibC is added as a reference in LibB. And class B uses class C. Using MEF, I have exported class B from LibB.

[Export(typeof(InterfaceForB))]
class B : InterfaceForB
{
  public IEnumerable<X> FuncInB()
  {
    return new C().FuncInC();
  } 
}

In class A, i am using the exported class from B, as follows.

public class A : InterfaceForA
{
    [Import(typeof(InterfaceForB))]
    private InterfaceForB _b;

    private CompositionContainer _container;

    public A()
    {
      var _catalog = new DirectoryCatalog(System.IO.Directory.GetCurrentDirectory());
      _container = new CompositionContainer(_catalog);
      _b = _container.GetExportedValue<InterfaceForB>();
    }

    public IEnumerable<X> FuncInA()
    {
      return _b.FuncInB();
    }
}

When i run FuncInA(), it raises FileNotFoundException with the following details:

"Could not load file or assembly 'LibC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."

Note:

  1. LibC reference exists in LibB, and it is build without errors.
  2. And all the assemblies (output dlls in this case) exist in the the same folder.
  3. If I comment the code "return new C().FuncInC();" in FuncInB() definition, & return a dummy object, it works without errors. The problem is because of the reffered LibC use.

Solution

  • In the LibB References shown in the solution explorer, right click on LibC, "properties", set "Specific Version" to "False".

    Or better yet, delete the binary reference and replace it by a project reference (assuming that LibC is in the same solution as LibB).