c# – 使用Simple Injector注入MVC​​ Controller Constructor时未注册参数

前端之家收集整理的这篇文章主要介绍了c# – 使用Simple Injector注入MVC​​ Controller Constructor时未注册参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个非常基本的问题,使用Simple Injector将依赖项注入MVC​​控制器.我以前使用过SimpleMap的Simple Injector.

MVC的版本是4.5,它是从NuGet安装的Simple Injector的最新版本.

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

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.

控制器如下:

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 Project / Assembly中.这些由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);
}

这是堆栈跟踪:

[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,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

我不知道为什么它不能正常工作,因为所有代码都是根据Simple Injector快速入门指南.

解决方法

确保HomeController引用的IContext实际上与您在Application_Start中注册的IContext的类型相同.最有可能的是,这是一种不同的类型.在注册中的IContext和HomeController中的IContext中对Visual Studio执行’goto definition’,可以确认Visual Studio是否跳转到同一个文件.

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

当您的配置实际包含具有相同名称的不同类型时,较新版本的Simple Injector实际上会警告您一个非常具体的错误,因此使用Simple Injector检测这些问题应该非常容易.当这种情况由不正确的动态程序集加载引起时,异常消息is even more specific.

猜你在找的C#相关文章