2012-02-16 77 views
1

我是通过ASP.NET MVC 3的DI新手,我使用的是mefcontrib作为我的DI容器。我通过nuget安装了mefcontribmefcontrib.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的回答。

回答

0

最好和最快的DI容器是StructureMap。我建议你使用它。如果你想使用它,我可以帮你配置

+0

did StructureMap是否支持开放式泛型? – 2012-02-16 12:08:03

+0

是的,它支持 – Yorgo 2012-02-16 13:32:50

+0

我切换到'StructureMap':D,我的问题现在解决了。但是如果我需要你的帮助,我会向你发出我的问题。感谢先进的呈现'StructureMap'。问候。 – 2012-02-16 13:53:17

相关问题