Parameter not registered when using Simple Injector to inject into an MVC Controller Constructor

10,019

Make sure that the IContext referenced by the HomeController is in fact the same type as the IContext that you registered in the Application_Start. Most likely, this is a different type. Doing a 'goto definition' into Visual Studio both on the IContext in the registration and the IContext in the HomeController allows you to confirm if Visual Studio jumps to the same file.

Another thing to check is whether the Container instance shown in your code is the actual (and only) container that is registered in MVC. Go search for any other new Container registrations in your application.

Newer versions of Simple Injector actually warn you with a very specific error when your configuration actually contains a different type with the same name, so it should be very easy to detect these problems with Simple Injector. And when this happens caused by incorrect dynamic assembly loading, the exception message is even more specific.

Share:
10,019
Graham
Author by

Graham

Updated on June 17, 2022

Comments

  • Graham
    Graham almost 2 years

    I'm have what seems to be a very basic problem using Simple Injector to inject dependencies into an MVC Controller. I am new to using Simple Injector having used StructureMap previously.

    The version of MVC is 4.5 and it is the latest version of Simple Injector installed from NuGet.

    The error I am getting when viewing the /Index view of the HomeController is:

    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.

    The controller is as so:

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

    The IContext is just a simple interface:

    public interface IContext
    {
    }
    

    And the concrete IContext implementation also simple and is just a wrapper for a regular DbContext.

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

    For info, the IContext interface lives in a different VS Project/Assembly to the DbContext implementation. These are referenced by the MVC project.

    I have the following in my 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);
    }
    

    This is the stack trace:

    [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
    

    I have no idea why it isn't working as the code all is as per the Simple Injector quick start guide.

  • Graham
    Graham about 10 years
    Thanks for the reply Steven. The IContext is definitely the same type and there is only one definition across all assemblies. The error is being thrown on all types being injected. I stripped everything back and tested it with the simplest case I could find (in the case using the IContext).
  • Steven
    Steven about 10 years
    Is there perhaps a second HomeController? Could you check by replacing the line container.RegisterMvcControllers(...) with container.Register<HomeController>()?
  • Graham
    Graham about 10 years
    There is only one controller in the project (the only code of note in the whole MVC project is as posted above). I did just try replacing that line though and it's made no difference. :s
  • Graham
    Graham about 10 years
    Thanks Steven. In stripping down further I've noticed that when installing from NuGet, the installer added a SimpleInjectorInitializer.cs file to App_Start. Removing this has fixed the problem. Thanks for your kind help though :)
  • Steven
    Steven about 10 years
    @Graham: Ahh.. so you had a second -and most of all: empty- container that was registered. I'll update my answer to reflect this.