1

私はasp.net mvc 3アプリケーションでMEFを使用しようとしていますが、私は注入を実現できませんでした。ここでは、コードは次のようになります。asp.net mvcでmefを使用する3

私が持っているインターフェースクラスライブラリを持っている:

namespace Namespace.Interfaces 
{ 
    public interface IMessenger 
    { 
     string GetMessage(); 
    } 
} 

そして、私はクラスがIMessengerから継承する必要があり、別のクラスライブラリで:

namespace Namespace.Message 
{ 
    [Export(typeof(IMessenger))] 
    public class Messenger : IMessenger 
    { 
     public Messenger() 
     { 

     } 

     public string GetMessage() 
     { 
      return "Mef Test!"; 
     } 
    } 
} 

私は「部品を持っています"ディレクトリにあり、" Messenger "というクラスを含むクラスライブラリのdllをコピーしています。ここで

private static CompositionContainer _container; 

    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 

     DirectoryCatalog catalog = new DirectoryCatalog(Server.MapPath("Plugins"), "*.dll"); 
     _container = new CompositionContainer(catalog); 
     _container.ComposeParts(); 

     DependencyResolver.SetResolver(new MefDependencySolver(_container)); 
    } 

がMefDependencyResolverクラスです:Global.asaxの中

私が持っている

public class MefDependencySolver : IDependencyResolver 
{ 
    public MefDependencySolver(CompositionContainer compositionContainer) 
    { 
     this.compositionContainer = compositionContainer; 
    } 

    private CompositionContainer compositionContainer; 

    public object GetService(Type serviceType) 
    { 
     string name = AttributedModelServices.GetContractName(serviceType); 

     return compositionContainer.GetExportedValueOrDefault<object>(name); 
    } 

    public IEnumerable<object> GetServices(Type serviceType) 
    { 
     return this.compositionContainer.GetExportedValues<object>(serviceType.FullName); 
    } 
} 

そして最後に、私はそのような私のコントローラでそれを使用しようとしている:

public class HomeController : Controller 
{ 
    [ImportAttribute] 
    public IMessenger _messenger { get; set; } 

    public HomeController() 
    { 

    } 

    public ActionResult Index() 
    { 
     ViewBag.Message = _messenger.GetMessage(); 

     return View(); 
    } 
} 

ただし、_messengerオブジェクトは作成されません。私は、オブジェクトのインスタンスに設定されていないオブジェクト参照を取得しています。私は間違って何をしていますか?事前に

おかげで、あなたがそのように、あなたのGetServices方法書き換える必要があるすべての

+0

私は助けが必要です。全く分からない? – anilca

+0

MEF 2でこのシナリオを簡略化するために*ロット*を実行しました。現在CodePlexバージョンのみが利用可能ですが、フィードバックがあれば素晴らしいでしょう! http://blogs.msdn.com/b/bclteam/archive/2011/11/11/using-mef-2-with-asp-net-mvc-3.aspx –

答えて

1

:まず重要なのは何

public IEnumerable<object> GetServices(Type type) 
    { 
     string name = AttributedModelServices.GetContractName(type); 

     try 
     { 
      return container.GetExportedValues<object>(name); 
     } 
     catch 
     { 
      return new object[] {}; 
     } 
    } 

サービスが解決できない場合は空のリストを返すことです。

第2のことは、プロパティではなくコンストラクタによる注入を優先することです。 IMessengerの依存関係が必要なので、コンストラクタによってそれを求めてください。私は、問題は、このライン_container.ComposeParts();から来ていると思い

public HomeController : Controller 
{ 
    private readonly IMessenger _messenger; 

    [ImportingConstructor] 
    public HomeController(IMessenger messenger) 
    { 
     _messenger = messenger; 
    } 

    // other actions 
} 

:あなたはこのようにそれをrevriteすることができます。私はあなたがそれを呼び出すべきではないと思います。 MEFがあなたのためにやっています。

+0

あなたの答えをありがとう。私のコードを変更した後、今は "このオブジェクトのために定義された無パラメータコンストラクタ" HomeControllerの例外があります。パラメータのないコンストラクタを追加することはあまり役に立ちません。それは、パラメータのないコンストラクタを呼び出すことを主張し、オブジェクト参照のインスタンスに設定されていないオブジェクト参照をスローします。 – anilca

+1

HomeControllerに[Export]属性を入れてみてください。 MEFは、その型を構築できるかどうかを知る必要があります。 –

+0

再度同じ例外があります: "このオブジェクトに対して定義されたパラメータのないコンストラクタはありません" – anilca

関連する問題