【问题标题】:The IControllerFactory 'MefContrib.Web.Mvc.CompositionControllerFactory' did not return a controller for the name 'Home'IControllerFactory 'MefContrib.Web.Mvc.CompositionControllerFactory' 没有返回名称为 'Home' 的控制器
【发布时间】:2012-02-16 08:15:07
【问题描述】:

我是通过 ASP.NET MVC 3 接触 DI 的新手。我使用 mefcontrib 作为我的 DI 容器。我通过 nuget 安装了 mefcontribmefcontrib.mvc3。当我尝试运行该项目时,出现此错误:

IControllerFactory 'MefContrib.Web.Mvc.CompositionControllerFactory' 没有返回 名称为“Home”的控制器。

我用谷歌搜索了错误并再次搜索。但我什么也找不到!大家可以帮帮我吗?这里是 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 的回答。

【问题讨论】:

    标签: asp.net-mvc-3 generics c#-4.0 dependency-injection mef


    【解决方案1】:

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

    【讨论】:

    • StructureMap 是否支持开放泛型?
    • 我切换到StructureMap :D,我的问题现在解决了。但是如果我需要你的帮助,我会把我的问题发给你。提前感谢您提供StructureMap。问候。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2020-07-06
    相关资源
    最近更新 更多