我正在创建一个带有MEF和Unity集成的Microsft MVC4应用程序来处理插件架构(其他MVC4项目)及其依赖性。但是,在尝试选择包含Unity依赖性的MEF导入控制器时,我收到错误。这是错误:
1)无法创建“X”类型的实例,因为无法选择构造函数进行构造。确保该类型具有默认构造函数或标记为“System.ComponentModel.Composition.ImportingConstructorAttribute”的单个构造函数。
主要的MVC4应用程序MEF + Unity集成代码:
// Create MEF catalog from plugin directory.
var aggregateCatalog = new AggregateCatalog(new DirectoryCatalog(HostingEnvironment.MapPath("~/bin"), "*.dll"));
// Setup Unity container based on catalog.
var unityContainer = new UnityContainer();
unityContainer.RegisterCatalog(aggregateCatalog);
// Register dependancy.
unityContainer.RegisterType<ISystemService, SystemService>();
// Compose the MEF container.
CompositionContainer container = new CompositionContainer(aggregateCatalog, true);
container.ComposeParts(aggregateCatalog.Catalogs);
// Register the controller factory that will handle requests.
ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(container));
var dependencyResolver = System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver;
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new MefDependencyResolver(container);
CreateController方法从MEF中选择适当的控制器
// Get all the exported controllers.
var exports = _container.GetExports<IController, INameMetadata>();
// Select controller based off meta data.
var controller = exports.Where(e => e.Metadata.Name.ToLower().Equals(controllerName.ToLower())
&& e.Metadata.Module.ToLower().Equals(module.ToLower()))
.Select(e => e.Value).FirstOrDefault();
设置控制器我正在尝试导入。它是抽象基类的子类。
基础课程。
public abstract class WidgetController : Controller
{
[Dependency]
public ISystemService _systemService { get; set; }
[ImportingConstructor]
public WidgetController(ISystemService systemService)
{
_systemService = systemService;
}
}
儿童班。
[InheritedExport(typeof(IController))]
[ExportMetadata("Module", "TheComponent")]
[ExportMetadata("Name", "TheName")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class WidgetXController : WidgetController
{
public WidgetXController (ISystemService systemService)
: base(systemService)
{
}
}
使用此结构,WidgetXController可用作CreateController方法中的导出。但是,当尝试在LINQ查询中选择它时,我得到上面的错误消息。我尝试将[ImportingConstructor]属性添加到子类,但这会导致控制器进入导出列表。我认为在MEF + Unity集成中没有正确设置某些内容......
有没有人对MEF + Unity集成功能如何帮助解决我看到的错误有任何见解?
答案 0 :(得分:1)
我找到了解决方案。我需要从Unity获取它,而不是创建一个新的合成容器。
原始代码:
// Compose the MEF container.
CompositionContainer container = new CompositionContainer(aggregateCatalog, true);
container.ComposeParts(aggregateCatalog.Catalogs);
// Register the controller factory that will handle requests.
ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(container));
var dependencyResolver = System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver;
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new MefDependencyResolver(container);
变为:
// Obtain the MEF container from Unity
var mefContainer = unityContainer.Resolve<CompositionContainer>();
mefContainer.ComposeParts(this);
// Register the controller factory that will handle requests.
ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(mefContainer));
var dependencyResolver = System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver;
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new MefDependencyResolver(mefContainer);