2012-10-23 74 views
2

我試圖將微軟可擴展性框架(MEF)實現爲一個示例基於MVC的web應用程序。我在MEF Overview頁面上使用SimpleCalculator示例解決方案。我的目標是可以從另一個項目動態加載DLL擴展的應用程序,以擴展模型的功能,本質上我希望MVC-Application成爲其他插件擴展的框架。首先我的設置:難倒MVC MEF應用程序

項目1(MVC應用中,MEF組件主機):

我裝點在我的模型的要素如下:

using System.ComponentModel.Composition; 
using System.ComponentModel.Composition.Hosting; 

namespace ExpressionParserPOC.Models 
{ 

    public class ExpressionModel 
     { 
      private CompositionContainer _container; 

      [ImportMany] 
      IEnumerable<Lazy<IExtensions,IExtensionName>> extensions { get; set; } 

     public ExpressionModel() 
     { 
      var catalog = new AggregateCatalog(); 
      catalog.Catalogs.Add(new DirectoryCatalog("C:\\local_visual_studio\\ExpressionParserPOC\\ExpressionParserPOC\\Extensions")); 

      //Create the CompositionContainer with the parts in the catalog 
      _container = new CompositionContainer(catalog); 
      _container.ComposeParts(_container); 

      //TEST: Can we access the extensions? 
      if (extensions != null)//<--NULL WHEN CONSTRUCTOR IS CALLED WHY? Expected GetMatrix() Function from DLL to be exposed 
      { 
      foreach (Lazy<IExtensions, IExtensionName> i in extensions) 
      { 
       Lazy<IExtensions, IExtensionName> foo = i; 
      } 
      } 

     } 

     } 

    public interface IExtensions 
    { 
    double[][] GetMatrix(string matrix); 
    } 

    public interface IExtensionName 
    { 
    Char Symbol { get; } 
    } 

} 

現在要進行測試,我只是調用用於從所述控制器模型的構造:

namespace ExpressionParserPOC.Controllers 
{ 
    public class HomeController : Controller 
    { 
    public ActionResult Index() 
    { 
     ViewBag.Message = "Enter an Expression for Octave to evaluate"; 

     //Instantiate an ExpressionModel composed of extensible parts 
     var model = new ExpressionModel(); 
     return View(model); 
    } 

    } 
} 

計劃2(DLL項目,MEF成分):

//PROJECT BUILD OUTPUTS TO THE EXTENSIONS DIRECTORY IN PROJECT 1 ABOVE 
using System.ComponentModel.Composition; 

namespace MyExtensions 
{ 
    //Project 1 already added as a reference 
    [Export(typeof(ExpressionParserPOC.Models.IExtensions))] 
    [ExportMetadata("Symbol", 'o')] 
    public class Octave : MyExtensions.IExtensions 
    { 

     //Other class properties, constructors, functions, etc. 

    #region FUNCTIONS 
    public double[][] GetMatrix(string matrix) 
    { 
     double[][] mat = new double[][]; 
     //Do Stuff 
     return mat; 
    } 
    #endregion 

    } 

    public interface IExtensions : ExpressionParserPOC.Models.IExtensions 
    { 

    } 

} 

問題是,當我從同一個項目中的Controller調用Model構造函數時,宿主MVC應用程序項目中的擴展List永遠不會被填充。我是MEF開發的新手,不確定自己在做什麼錯,是否有額外的需要考慮,因爲我正在使用MVC應用程序?爲什麼Project 1擴展列表不會被填充?我的界面定義是否錯誤?示例計算器應用程序是一個簡單的命令行項目,似乎工作正常,我看到的唯一區別是外部DLL項目是在相同的解決方案空間,而與我的例子解決方案空間是獨立的。

回答

1

構造函數ExpressionModel中存在一個微不足道的錯誤。您正在撰寫的容器本身:)

_container.ComposeParts(_container); 

這個問題應該是顯而易見的瞬間,因爲如果ExpressionModel的組成實際發生過,即使沒有擴展名被發現後,extensions屬性將有0項進行初始化,但它不會是null

您需要撰寫當前模型的實例是這樣的:

_container.ComposeParts(this); 

一切似乎是正常的,應該是這個修正後工作正常。