1

我正在创建一个带有 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);

从 MEF 中选择合适控制器的 CreateController 方法

// 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 集成功能如何帮助解决我看到的错误有任何见解?

4

1 回答 1

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);
于 2013-09-03T14:26:48.797 回答