私はASP.NET MVC 3を使用してDIを初めて使いました。私はDIコンテナとしてmefcontrib
を使用しています。私はmefcontrib
とmefcontrib.mvc3
をナゲット経由でインストールします。プロジェクトを実行しようとすると、次のエラーが表示されます。IControllerFactory 'MefContrib.Web.Mvc.CompositionControllerFactory'は、 'Home'という名前のコントローラを返しませんでした。
The IControllerFactory 'MefContrib.Web.Mvc.CompositionControllerFactory' did not return a controller for the name '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();
}
}
}
UPDATE:
@YorgoはStructureMap
に切り替えるために私を提案し、私はそれを行います。 StructureMap
は私にとっては簡単なので、@ Yorgoの答えを受け入れる。
StructureMapはオープンジェネリックをサポートしていますか? –
はいそれは – Yorgo
をサポートします。私は 'StructureMap'に切り替わります:Dと私の問題は今解決されました。しかし、私があなたの助けを必要とするなら、私はあなたに私の質問をお送りします。 'StructureMap'を発表してくれてありがとう。よろしく。 –