导出'XYZ.IMessageCenterBL (ContractName =“XYZ.IMessageCent

本文关键字:XYZ ContractName IMessageCent IMessageCenterBL 导出 | 更新日期: 2023-09-27 18:01:52

与其他关于出口不属于特定类型的问题不同,这个问题完全不同。

类型XYZ.IMessageCenterBLSystem.Web.Mvc.JsonResult是完全不同的,我们不可能混淆它们。

此外,如果我尝试使用System.Web.Mvc.JsonResult而不是XYZ.IMessageCenterBL,代码将无法编译。

现在XYZ.IMessageCenterBL被注入到属性中:

[Import]
public IMessageCenterBL MessageCenterBL { get; set; }

注意,这里没有任何混乱的地方——这个JsonResult业务是从哪里来的?

同样,如果我将依赖从属性移动到构造函数中(即IMessageCenterBL被注入构造函数并从那里显式设置属性),它开始工作!

下面是整个MEF异常输出:

Unhandled Exception at url: http://localhost:51000/u/2s4xYN1VikulrQAi44nvWg/Framework/XmlForm/GetTemplate 
System.InvalidOperationException: An error occurred when trying to create a controller of type 'DF.Web.Framework.Controllers.XmlForms.XmlFormController`1[DF.Data.WorkFlow.WF_Employee]'. Make sure that the controller has a parameterless public constructor. ---> System.ComponentModel.Composition.CompositionException: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) The export 'DF.Web.Framework.Business.IMessageCenterBL (ContractName="DF.Web.Framework.Business.IMessageCenterBL")' is not assignable to type 'System.Web.Mvc.JsonResult'.
Resulting in: Cannot set import 'DF.Web.Platform.DFController.Data (ContractName="DF.Web.Framework.Business.IMessageCenterBL")' on part 'DF.Web.Framework.Controllers.XmlForms.XmlFormController(DF.Data.WorkFlow.WF_Employee)'.
Element: DF.Web.Platform.DFController.Data (ContractName="DF.Web.Framework.Business.IMessageCenterBL") -->  DF.Web.Framework.Controllers.XmlForms.XmlFormController({0}) -->  AssemblyCatalog (Assembly="DF.Web.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93")
Resulting in: Cannot get export 'DF.Web.Framework.Controllers.XmlForms.XmlFormController(DF.Data.WorkFlow.WF_Employee) (ContractName="DF.Web.Framework.Controllers.XmlForms.XmlFormController(DF.Data.WorkFlow.WF_Employee)")' from part 'DF.Web.Framework.Controllers.XmlForms.XmlFormController(DF.Data.WorkFlow.WF_Employee)'.
Element: DF.Web.Framework.Controllers.XmlForms.XmlFormController(DF.Data.WorkFlow.WF_Employee) (ContractName="DF.Web.Framework.Controllers.XmlForms.XmlFormController(DF.Data.WorkFlow.WF_Employee)") -->  DF.Web.Framework.Controllers.XmlForms.XmlFormController({0}) -->  AssemblyCatalog (Assembly="DF.Web.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93")
   at System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition)
   at System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(CatalogPart part, ExportDefinition export, Boolean isSharedPart)
   at System.ComponentModel.Composition.Hosting.CatalogExportProvider.CatalogExport.GetExportedValueCore()
   at System.ComponentModel.Composition.Primitives.Export.get_Value()
   at System.ComponentModel.Composition.ExportServices.GetCastedExportedValue[T](Export export)
   at System.ComponentModel.Composition.ReflectionModel.ExportFactoryCreator.LifetimeContext.GetExportLifetimeContextFromExport[T](Export export)
   at System.ComponentModel.Composition.ReflectionModel.ExportFactoryCreator.<>c__DisplayClass4`1.<CreateStronglyTypedExportFactoryOfT>b__3()
   at System.ComponentModel.Composition.ExportFactory`1.CreateExport()
   at DF.Web.Platform.MefDependencyResolver.CacheItem`1.GetService(CompositionContainer compositionContainer) in c:'DF'SharpTop'UI'Platform'MefDependencyResolver.cs:line 41
   at DF.Web.Platform.MefDependencyResolver.System.Web.Mvc.IDependencyResolver.GetService(Type serviceType) in c:'DF'SharpTop'UI'Platform'MefDependencyResolver.cs:line 100
   at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
   --- End of inner exception stack trace ---
   at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

MEF是如何混淆这两种类型的?

导出'XYZ.IMessageCenterBL (ContractName =“XYZ.IMessageCent

我刚刚重新测试了整个问题,现在它工作正常。我知道它是由两个独立的开发者在两台电脑上复制的,所以我无法解释这种奇怪的行为。