我是通过ASP.NET MVC 3的DI新手。我使用mefcontrib
作为我的DI容器。我通过nuget安装了mefcontrib
和mefcontrib.mvc3
。当我尝试运行该项目时,我收到此错误:
IControllerFactory 'mefContrib.Web.Mvc.CompositionControllerFactory'没有返回 控制器名为'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();
}
}
}
更新
@Yorgo建议我切换到StructureMap
然后我这样做。 StructureMap
对我来说更容易,所以我接受@ Yorgo的回答。
答案 0 :(得分:0)
最佳和最快的DI容器是StructureMap。我建议你使用它。如果您想使用它,我可以帮助您进行配置