我是通過ASP.NET MVC 3的DI新手,我使用的是mefcontrib
作爲我的DI容器。我通過nuget安裝了mefcontrib
和mefcontrib.mvc3
。當我嘗試運行該項目時,出現此錯誤:IControllerFactory的'MefContrib.Web.Mvc.CompositionControllerFactory'沒有返回名稱'Home'的控制器
The IControllerFactory 'MefContrib.Web.Mvc.CompositionControllerFactory' did not return a controller for the name 'Home'.
我使用了Google搜索錯誤並重新搜索和搜索。但我找不到任何東西!大家可以幫助我嗎?這裏是自動的NuGet創建App_Start
內容:
[assembly: WebActivator.PreApplicationStartMethod(typeof(AlamKouh.UI.App_Start.AppStart_MefContribMVC3), "Start")]
namespace AlamKouh.UI.App_Start
{
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web.Mvc;
using MefContrib.Hosting.Conventions;
using MefContrib.Web.Mvc;
public static class AppStart_MefContribMVC3
{
public static void Start()
{
CompositionContainerLifetimeHttpModule.Register();
var catalog = new AggregateCatalog(
new DirectoryCatalog("bin"),
new ConventionCatalog(new MvcApplicationRegistry()));
var dependencyResolver = new CompositionDependencyResolver(catalog);
DependencyResolver.SetResolver(dependencyResolver);
ControllerBuilder.Current.SetControllerFactory(
new CompositionControllerFactory(
new CompositionControllerActivator(dependencyResolver)));
FilterProviders.Providers.Remove(FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider));
FilterProviders.Providers.Add(new CompositionFilterAttributeFilterProvider(dependencyResolver));
ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().Single());
ModelValidatorProviders.Providers.Add(
new CompositionDataAnnotationsModelValidatorProvider(dependencyResolver));
ModelBinderProviders.BinderProviders.Add(
new CompositionModelBinderProvider(dependencyResolver));
}
}
}
,這是我的HomeController:
namespace AlamKouh.UI.Controllers {
public interface ITest<T> { }
[Export(typeof(ITest<>))]
public class Test<T> : ITest<T> { }
public class HomeController : Controller {
private readonly ITest<string> _test;
[ImportingConstructor]
public HomeController(ITest<string> test) {
_test = test;
}
public ActionResult Index() {
var t = _test;
return View();
}
}
}
UPDATE:
@Yorgo建議我改用StructureMap
和我做。 StructureMap
對我來說比較容易,所以我接受@Yorgo的回答。
did StructureMap是否支持開放式泛型? – 2012-02-16 12:08:03
是的,它支持 – Yorgo 2012-02-16 13:32:50
我切換到'StructureMap':D,我的問題現在解決了。但是如果我需要你的幫助,我會向你發出我的問題。感謝先進的呈現'StructureMap'。問候。 – 2012-02-16 13:53:17