mvc 4 mef导入/导出混乱

时间:2013-04-09 18:31:50

标签: asp.net-mvc asp.net-mvc-4 mef

我很难将我的头脑包裹在Mef周围以及进出口的运作方式。我的项目结构如下。

Projects:
MefMVPApp (Main MVC 4 app)
MefMVCFramework.Common(Interfaces shared between the projects)
MefMVCDemo.Plugins.OrderStatus (pluggable area.)
MefMVCDemo.Plugins.Data (Repository for OrderStatus)
OrderStatus.Models(domain models shared between the projects)

主要Mvc应用程序的目标是通过mef。

来托管可插拔区域

OrderStatus Area有一个名为OrderStatusController的控制器,并使用Export Attribute和ImportingConstructor进行修饰。

[Export(typeof(IController))]
        [ExportMetadata("controllerName", "OrderStatus")]
        [PartCreationPolicy(CreationPolicy.NonShared)] 
        public class OrderStatusController : Controller
        {
            private readonly IRepository<OrderStatusApp.OrderStatusResponse>_repository ;
            [ImportingConstructor]
            public OrderStatusController(IRepository<OrderStatusApp.OrderStatusResponse> oRepository)
            {
                _repository = oRepository;
            }
            public ActionResult Index()
            {
                var model = _repository.GetAll();
                return View();
            } 
    }

IRepository是MefMVCFramework.Common程序集中的一个类,将用于通用CRUD操作。

public interface IRepository<T> where T : class
            {
               IEnumerable<T> GetAll();
               T GetById(int id);
               void Add(T entity);
               int SaveOrUpdate(T entity);
                bool Delete(T entity);
                bool Delete(int id); 
            }

MefMVCDemo.Plugins.Data程序集包含一个名为OrderManagementRepository的类,该类用于通用存储库,并标有导出属性。

[Export(typeof(IRepository<OrderStatusApp.OrderStatusResponse>))]
            [PartCreationPolicy(CreationPolicy.NonShared)] 
                public class OrderManagementRepository  : IRepository<OrderStatusApp.OrderStatusResponse>
                {
                    private readonly JsonServiceClient _client;

                    public OrderManagementRepository()
                    {
                        _client = new JsonServiceClient("http://localhost:52266");
                    }
                    public IEnumerable<OrderStatusApp.OrderStatusResponse> GetAll()
                    {

                        throw new NotImplementedException("Can not get all");
                    }
                    public OrderStatusApp.OrderStatusResponse GetById(int id)
                    {
                        throw new NotImplementedException();
                    }
                    public void Add(OrderStatusApp.OrderStatusResponse entity)
                    {
                        throw new NotImplementedException();
                    }
                    public int SaveOrUpdate(OrderStatusApp.OrderStatusResponse entity)
                    {
                        throw new NotImplementedException();
                    }
                    public bool Delete(OrderStatusApp.OrderStatusResponse entity)
                    {
                        throw new NotImplementedException();
                    }
                    public bool Delete(int id)
                    {
                        throw new NotImplementedException();
                    }
                } 

使用Mefx工具,我可以看到我的部件而且没有拒绝。

mefx /dir:C:\
Source.PreBranch.Keep\Prototypes\Projects\MefDemoApp\mefMVC4App\bin /parts
MefMVCDemo.Plugins.Data.OrderManagementRepository
mefMVCDemo.Plugins.OrderStatus.Controllers.OrderStatusController
MefMVCDemo.Plugins.OrderStatus.Verbs.OrderStatusVerb

我可以看到我的导入。

mefx /dir:C:\
Source.PreBranch.Keep\Prototypes\Projects\MefDemoApp\mefMVC4App\bin /imports
MefMVCFramework.Common.IRepository(OrderStatus.Models.OrderStatusApp+OrderStatus
Response)
MefMVCFramework.Common.IRepository(OrderStatus.Models.OrderStatusApp+OrderStatus
Response)

现在用/ orderstatus uri浏览我的主要mvc网站时出现以下错误: 没有为此对象定义无参数构造函数。

向OrderStatusController添加一个不带重载的默认构造函数似乎不起作用。

我想问题是我做错了什么?为什么构造函数中的接口最终都为null,为什么有关于“为此对象定义的无参数构造函数”的mvc错误。

2 个答案:

答案 0 :(得分:1)

MVC中的默认控制器工厂尝试使用无参数构造函数创建控制器。如果要更改此行为,则需要创建自己的自定义控制器工厂。

使用控制器上的导入/导出的ControllerFactory的

Here is an example

我正在使用MEF将某些部件导入我的应用程序,但我的控制器未导入/导出,因此我创建了以下控制器工厂

public class ControllerFactory : IControllerFactory
{
    private readonly CompositionContainer _container;
    private IControllerFactory _innerFactory;

/// <summary>
/// Constructor used to create the factory
/// </summary>
/// <param name="container">MEF Container that will be used for importing</param>
public ControllerFactory(CompositionContainer container)
{
    _container = container;
    _innerFactory = new DefaultControllerFactory();
}

/// <summary>
/// Method used for create the controller based on the provided name. It calls the
/// constructor of the controller passing the MEF container
/// </summary>
/// <param name="requestContext">Context of the request</param>
/// <param name="controllerName">Name of the controller provided in the route</param>
/// <returns>The controller instance</returns>
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
    Type controllerType = FindControllerByName(controllerName);

    var args = new object[] { this._container };
    var controller = (IController)Activator.CreateInstance(controllerType, args);

    return controller;
}

/// <summary>
/// This methods looks into the current Assembly for the Controller type
/// </summary>
/// <param name="name">The controller name provided in the route</param>
/// <returns>The controller type</returns>
private static Type FindControllerByName(string name){
    var a = Assembly.GetAssembly(typeof(ControllerFactory));
    var types = a.GetTypes();
    Type type = types.Where(t => t.Name == String.Format("{0}Controller", name)).FirstOrDefault();              

    return type;
}


public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
{
    return System.Web.SessionState.SessionStateBehavior.Default;
}

public void ReleaseController(IController controller)
{
    var disposableController = controller as IDisposable;
    if (disposableController != null)
    {
        disposableController.Dispose();
    }
}

}

答案 1 :(得分:1)

感谢pollirrata指出我正确的方向。

我不得不改变一些事情才能让它发挥作用。

1。)我在我的MefMVCFramework.Common项目中添加了一个名为INameMetadata的接口。

public interface INameMetadata
{
    string Name { get; }
}

2.。)我的控制器导出的My ExportMetadata标签被修改为Name,OrderStatus。

 [Export(typeof(IController))]
[ExportMetadata("Name", "OrderStatus")]
[PartCreationPolicy(CreationPolicy.NonShared)] 
public class OrderStatusController : Controller
{
    private IRepository<OrderStatusApp.OrderStatusResponse> _repository;

    [ImportingConstructor]
    public OrderStatusController(IRepository<OrderStatusApp.OrderStatusResponse> oRepository)
    {
        _repository = oRepository;

    }

    public ActionResult Index()
    {
        var model = _repository.GetById(47985);
        return View(model);
    } 
}

3。)创建了MefControllerFactory(基于发布但已修改的pollirrata以查找元数据)

 public class MefControllerFactory : IControllerFactory
{
    private string _pluginPath;
    private readonly DirectoryCatalog _catalog;
    private readonly CompositionContainer _container;
    private DefaultControllerFactory _defaultControllerFactory;

    public MefControllerFactory(string pluginPath)
    {
        _pluginPath = pluginPath;
        _catalog = new DirectoryCatalog(pluginPath);
        _container = new CompositionContainer(_catalog);
        _defaultControllerFactory = new DefaultControllerFactory();
    }

    public MefControllerFactory(CompositionContainer compositionContainer)
    {

        _container = compositionContainer;
        _defaultControllerFactory = new DefaultControllerFactory();
    }
    #region IControllerFactory Members
    public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        //IController controller = null;
        var controller = _container.GetExports<IController,INameMetadata>()
            .Where(e=>e.Metadata.Name.Equals(controllerName))
            .Select(e=>e.Value).FirstOrDefault();

        if (controller == null)
        {
            throw new HttpException(404, "Not found");
        }

        return controller;

    }
    public void ReleaseController(IController controller)
    {
       var disposable = controller as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }
    #endregion


    public SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        return SessionStateBehavior.Default;
    }
}

4.。)我在Main MVC应用程序中创建了一个名为MefConfig的类,并将其移动到App_Start目录。

public static class MefConfig
{
    public static void RegisterMef()
    {
        //var builder = new RegistrationBuilder();
        //builder.ForTypesDerivedFrom<IRepository<OrderStatusApp.OrderStatusResponse>>().Export<IRepository<IRepository<OrderStatusApp.OrderStatusResponse>>>();

        var directoryCatalog = new DirectoryCatalog(HostingEnvironment.MapPath("~/bin"), "*.dll");

        var container = new CompositionContainer(directoryCatalog, true);
        ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(container));

        //Working
        //ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(HostingEnvironment.MapPath("~/bin")));

        // Install MEF dependency resolver for MVC
        var resolver = new MefDependencyResolver(container);
        DependencyResolver.SetResolver(resolver);
        // Install MEF dependency resolver for Web API
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
        var d = container.GetExportedValues<IRepository<OrderStatusApp.OrderStatusResponse>>();
        //Mefx.
        try
        {
            //var ci = new CompositionInfo(aggregateCatalog, container);
            var ci = new CompositionInfo(directoryCatalog, container);
            var partDef = ci.GetPartDefinitionInfo(typeof(IRepository<OrderStatusApp.OrderStatusResponse>));

            //var possibleCauses = partDef.FindPossibleRootCauses();
            var stringWriter = new StringWriter();
            CompositionInfoTextFormatter.Write(ci, stringWriter);
            var compStatString = stringWriter.ToString();
        }
        catch
        {

        }
        MvcApplication.ActionVerbs = container.GetExports<IActionVerb, IActionVerbMetadata>();
    }
}

5.。)从global.asax加载Mefconfig。

 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        //Register Mef 
        MefConfig.RegisterMef();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

    }