我在extensible asp.net mvc 3上使用了一個例子來構建我的可插入應用程序,但是我遇到了一個問題。在一個插件中,我聲明並實現了一個接口。導入不能解決asp.net mvc 3中需要的類型mef
但是,在插件控制器中,當我想使用這個類時,應用程序會拋出一個錯誤,並且似乎EntityConfig沒有被初始化。這怎麼解決?
[Export(typeof(IController)), ExportMetadata("controllerName", "Concept")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ConceptController : Controller
{
[Import(typeof(IEntityConfig))]
private IEntityConfig EntityConfig;
public ActionResult Index()
{
var obs = EntityConfig.EntityName;
return View("~/Bin/Views/Concept/Index.cshtml",obs);
}
}
public interface IEntityConfig
{
string EntityName { get;}
}
[Export(typeof(IEntityConfig))]
public class TestEntity : IEntityConfig
{
public string EntityName
{
get{return "Test";}
}
}
被修改: 在另一側上,當I是使用this例如,存在在解決EntityConfig沒有問題,但在該視圖中,當我想要加載模型如下:
@using Concepts
@model Concepts.Models.TestModel
應用程序會拋出一個錯誤,並告訴我「類型或名稱空間名稱'Concepts'找不到',儘管當我在容器啓動後檢查容器時,我可以在加載的程序集中看到Concepts。
你能幫我嗎? 謝謝。
編輯: 我上傳的樣品:
編輯(2011/22/09): 我測試上面的代碼上的其他sample該@ Matthew Abbott在他的博客中提供了它的工作原理,儘管這個示例是針對mvc 2.0構建的。
第一次嘗試的確切錯誤以及代碼中的哪一行是拋出的?你如何創建MEF'CompositionContainer'? –
感謝@WimCoenen考慮我的問題。對於第一個示例,我在包含以下內容的行中收到錯誤:「var obs = EntityConfig.EntityName;」錯誤是:「未將對象引用設置爲對象的實例」,用於創建「CompositionContainer」,我使用與示例中包含的代碼相同的代碼。你想讓我上傳嗎? –
你在使用Part1還是Part2? Example-Part2使用CompositeControllerFactory類。你可以查看它的單元測試,看看它的一些可能用法:https://github.com/moodmosaic/System.Web.Mvc.Composition/blob/master/Src/System.Web.Mvc.CompositionUnitTest/CompositeControllerFactoryFacts。 cs –