I\'m working on a bigger C# MVC 4 project which is divided in several assemblies (Core, Domain, Backend MVC, Frontend MVC etc.). I use the plugin architecture provided by M
I had the same problem, caused by loading same assembly multiple times. Adding you Contract to separated project and reference output dll instead of referencing project directly will solve this problem.
This looks similar to the System.InvalidCastException that is sometimes thrown when the same assembly is loaded twice in different contexes or from different locations. All assembly loading in MEF is being handled by the AssemblyCatalog class using the Assembly.Load(AssemblyName) method which will load the assembly with the given name in the Load context. However, under certain conditions, it will load it in the LoadFrom context and this can sometimes cause casting exceptions such as the one you mention:
Cannot cast the underlying exported value of type "XY.HomeController(ContractName="XY.HomeController")" to type "XY.HomeController".]
What I would do is look if the assembly that contains XY.HomeController
is deployed in more than one locations. Don't forget to look in the GAC if it is a strong named assembly.
A similar problem is mentioned in Telerik's forum.
If you want more details on this subject have a look at "How the Runtime Locates Assemblies" "Best Practices for Assembly Loading" as well as the Load-related entries in Suzanne Cooks MSDN blog.