C# 尝试将MEF与MVC控制器一起使用时出现CompositionContractMismatchException

C# 尝试将MEF与MVC控制器一起使用时出现CompositionContractMismatchException,c#,asp.net-mvc-4,mef,C#,Asp.net Mvc 4,Mef,我正在从事一个更大的C#MVC4项目,该项目分为几个程序集(核心、域、后端MVC、前端MVC等)。 我使用MEF提供的插件架构来加载和解决大多数依赖关系。现在我还想用它来加载MVC控制器。在几十个样本中发现了典型的场景 但我一直收到这样一封信: 例外情况是: [CompositionContractMismatchException: Cannot cast the underlying exported value of type "XY.HomeController (ContractNam

我正在从事一个更大的C#MVC4项目,该项目分为几个程序集(核心、域、后端MVC、前端MVC等)。 我使用MEF提供的插件架构来加载和解决大多数依赖关系。现在我还想用它来加载MVC控制器。在几十个样本中发现了典型的场景

但我一直收到这样一封信:

例外情况是:

[CompositionContractMismatchException: Cannot cast the underlying exported value of type "XY.HomeController (ContractName="XY.HomeController")" to type "XY.HomeController".]
System.ComponentModel.Composition.ExportServices.CastExportedValue(ICompositionElement element, Object exportedValue) +505573
System.ComponentModel.Composition.<>c__DisplayClass10`2.<CreateSemiStronglyTypedLazy>b__c() +62
System.Lazy`1.CreateValue() +14439352
System.Lazy`1.LazyInitValue() +91
  XY.DependencyManagement.SomeCustomControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) in (Path)\Core\DependencyManagement\SomeCustomControllerFactory.cs:32
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +89
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +305
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +87
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12550291
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
[CompositionContractMismatchException:无法将类型为“XY.HomeController(ContractName=“XY.HomeController”)”的基础导出值强制转换为类型为“XY.HomeController”。]
System.ComponentModel.Composition.ExportServices.CastExportedValue(ICompositionElement元素,Object exportedValue)+505573
System.ComponentModel.Composition.c_;u DisplayClass10`2.b_;u c()+62
System.Lazy`1.CreateValue()+14439352
System.Lazy`1.LazyInitValue()+91
(路径)\Core\DependencyManagement\SomeCustomControllerFactory.cs:32中的XY.DependencyManagement.SomeCustomControllerFactory.GetControllerInstance(RequestContext RequestContext,Type controllerType)
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext RequestContext,String controllerName)+89
ProcessRequestInit(HttpContextBase httpContext、IController和controller、IControllerFactory和factory)+305
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,异步回调,对象状态)+87
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+12550291
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+288
自定义ControllerFactory:

公共类SomeCustomControllerFactory:DefaultControllerFactory{
私有只读复合容器_复合容器;
公共SomeCustomControllerFactory(CompositionContainer CompositionContainer){
_compositionContainer=compositionContainer;
}
受保护的重写IController GetControllerInstance(RequestContext RequestContext,类型controllerType){
var export=_compositionContainer.GetExports(controllerType,null,null).SingleOrDefault();
i控制器结果;
如果(导出!=null){
结果=将.Value导出为IController;
}否则{
结果=base.GetControllerInstance(requestContext,controllerType);
_compositionContainer.ComposeParts(结果);
}
返回结果;
}
受保护的覆盖类型GetControllerType(RequestContext RequestContext,string controllerName){
类型controllerType=base.GetControllerType(requestContext,controllerName);
//用于查找容器中的对象,该容器中的程序集位于子目录中,且MVC未发现
//TODO:仅创建使用的零件
if(controllerType==null&&this.\u compositionContainer!=null&&
这是。_compositionContainer!=null){
变量控制器类型=
此.\u compositionContainer.GetExports()文件
.在哪里(
e=>
e、 Value.GetType().Name.ToLowerInvariant()==
controllerName.ToLowerInvariant()+ControllerNameByConvention)
.Select(e=>e.Value.GetType()).ToList();
开关(控制器类型.计数){
案例0:
controllerType=null;
打破
案例1:
controllerType=controllerTypes.First();
打破
案例2:
抛出CreateAmbiguousControllerException(requestContext.RouteData.Route、controllerName、,
控制器类型);
}
}
返回控制器类型;
}
和CustomDependencyResolver:

公共类CustomDependencyResolver:IDependencyResolver{
私有只读合成容器_容器;
公共CustomDependencyResolver(合成容器){
_容器=容器;
}
公共IDependencyScope BeginScope(){
返回(idependencycoscope)此;
}
公共对象GetService(类型serviceType){
var export=_container.GetExports(serviceType,null,null).SingleOrDefault();
返回null!=导出?导出。值:null;
}
公共IEnumerable GetServices(类型serviceType){
var exports=\u container.GetExports(serviceType,null,null);
var createdObjects=新列表();
if(exports.Any()){
foreach(导出中的var导出){
createdObjects.Add(export.Value);
}
}
返回CreatedObject;
}
一切都是这样配置的 SetResolver(新的CustomDependencyResolver(容器)); ControllerBuilder.Current.SetControllerFactory(新的SomeCustomControllerFactory(容器))

旁注:使用MEF2 RegistrationBuilder和AggregateCatalog,其中包含三个AssemblyCatalog和一个DirectoryCatalog

如果我将其从主项目解决方案中提取出来,创建一个新的MVC4 internet项目解决方案,并将其集成到其中,那么整个过程将非常完美

我已经打开CompositionOptions.DisableSilentRejection。并找到此资源以调试与MEF相关的错误 我删除了HomeController中的所有内容(空构造函数、无导入等)。MEF容器中充满了合适的导出。所有内容都很好

经过一整天的调试和研究,我学到了很多关于MEF的知识,但仍然有相同的问题。希望有人能给我一个错误的提示。因为将所有内容转移到新的MVC项目将非常耗时:-(

谢谢!

这看起来类似于在不同的上下文中或从中加载同一程序集两次时有时抛出的