C# 使用简单注入器注入MVC控制器构造函数时未注册参数

C# 使用简单注入器注入MVC控制器构造函数时未注册参数,c#,asp.net-mvc,asp.net-mvc-4,dependency-injection,simple-injector,C#,Asp.net Mvc,Asp.net Mvc 4,Dependency Injection,Simple Injector,我发现使用简单的注入器将依赖项注入到MVC控制器中似乎是一个非常基本的问题。我以前使用过StructureMap,现在才开始使用简单的喷油器 MVC的版本是4.5,它是从NuGet安装的简单喷油器的最新版本 查看HomeController的/Index视图时出现的错误是: HomeController类型的构造函数包含名为“context”的IContext类型参数,该参数未注册。请确保IContext已在容器中注册,或更改HomeController的构造函数 控制器如下所示: public

我发现使用简单的注入器将依赖项注入到MVC控制器中似乎是一个非常基本的问题。我以前使用过StructureMap,现在才开始使用简单的喷油器

MVC的版本是4.5,它是从NuGet安装的简单喷油器的最新版本

查看HomeController的/Index视图时出现的错误是:

HomeController类型的构造函数包含名为“context”的IContext类型参数,该参数未注册。请确保IContext已在容器中注册,或更改HomeController的构造函数

控制器如下所示:

public class HomeController : Controller
{
    public HomeController(IContext context) { }

    public ActionResult Index() { }
}
IContext只是一个简单的界面:

public interface IContext
{
}
具体的IContext实现也很简单,只是常规DbContext的包装器

public class DbContext : System.Data.Entity.DbContext, IContext
{ 

}
对于信息,IContext接口位于与DbContext实现不同的VS项目/程序集中。这些都被MVC项目引用

我的Global.asax.cs中有以下内容:

protected void Application_Start()
{
    var container = new Container();

    container.Register<IContext, DbContext>();

    container.RegisterMvcControllers(System.Reflection.Assembly.GetExecutingAssembly());

    container.RegisterMvcAttributeFilterProvider();

    container.Verify();

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

    // Regular MVC startup
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

我不知道它为什么不工作,因为代码都是根据简单喷油器快速启动指南编写的。

确保
HomeController
引用的
IContext
实际上与您在
应用程序启动中注册的
IContext
类型相同。很可能,这是另一种类型。通过在注册中的
IContext
HomeController
中的
IContext
上对Visual Studio执行“转到定义”,可以确认Visual Studio是否跳转到同一文件

要检查的另一件事是,代码中显示的
容器
实例是否是在MVC中注册的实际(也是唯一)容器。在应用程序中搜索任何其他
新容器
注册


当您的配置实际包含具有相同名称的不同类型时,Simple Injector的较新版本实际上会用一个非常具体的错误警告您,因此使用Simple Injector检测这些问题应该非常容易。如果这是由于动态程序集加载不正确导致的,则会显示异常消息。

感谢您的回复。IContext绝对是相同的类型,所有程序集中只有一个定义。正在注入的所有类型上引发错误。我把所有东西都剥了回去,用我能找到的最简单的案例(在使用IContext的案例中)对它进行了测试。是否有第二个
HomeController
?您是否可以将行
container.RegisterMvcControllers(…)
替换为
container.Register()
?项目中只有一个控制器(整个MVC项目中唯一值得注意的代码如上所述)。我只是试着换掉了那条线,但没什么不同汉克斯·史蒂文。在进一步剥离过程中,我注意到当从NuGet安装时,安装程序向App_Start添加了一个SimpleInjectorInitializer.cs文件。删除此选项已修复问题。谢谢你的帮助:)@Graham:ah。。所以你有了第二个——也是最重要的一个——注册的空容器。我将更新我的答案以反映这一点。
[ActivationException: The constructor of the type HomeController contains the parameter of type IContext with name 'context' that is not registered. Please ensure IContext is registered in the container, or change the constructor of HomeController.]
SimpleInjector.Advanced.DefaultConstructorInjectionBehavior.BuildParameterExpression(ParameterInfo parameter) +229
   SimpleInjector.Registration.BuildParameterExpressionFor(ParameterInfo parameter) +43
   SimpleInjector.Registration.BuildConstructorParameters(ConstructorInfo constructor) +170
   SimpleInjector.Registration.BuildNewExpression(Type serviceType, Type implementationType) +62
   SimpleInjector.Registration.BuildTransientExpression() +85
   SimpleInjector.Registration.BuildExpression(InstanceProducer producer) +62
   SimpleInjector.InstanceProducer.BuildExpressionInternal() +42
   System.Lazy`1.CreateValue() +14443208
   System.Lazy`1.LazyInitValue() +476
   SimpleInjector.InstanceProducer.BuildExpression() +159

 [ActivationException: The registered delegate for type HomeController threw an exception. The     constructor of the type HomeController contains the parameter of type IContext with name 'context' that is not registered. Please ensure IContext is registered in the container, or change the constructor of HomeController.]
   SimpleInjector.InstanceProducer.BuildExpression() +257
   SimpleInjector.InstanceProducer.VerifyExpressionBuilding() +53

[InvalidOperationException: The configuration is invalid. Creating the instance for type HomeController failed. The registered delegate for type HomeController threw an exception. The constructor of the type HomeController contains the parameter of type IContext with name 'context' that is not registered. Please ensure IContext is registered in the container, or change the constructor of HomeController.]
   SimpleInjector.InstanceProducer.VerifyExpressionBuilding() +161
   SimpleInjector.Container.VerifyIfAllExpressionsCanBeBuilt(InstanceProducer[] producersToVerify) +45
   SimpleInjector.Container.VerifyIfAllExpressionsCanBeBuilt() +166
   SimpleInjector.Container.Verify() +39
   MyMvcApp.App_Start.SimpleInjectorInitializer.Initialize() +216

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +229
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +193
    System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35       
 WebActivator.BaseActivationMethodAttribute.InvokeMethod() +341
   WebActivator.ActivationManager.RunActivationMethods() +534
   WebActivator.ActivationManager.RunPostStartMethods() +38
   WebActivator.StartMethodCallingModule.Init(HttpApplication context) +159
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +530
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +304
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +404
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475

[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12889028
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12730121