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

Graham picture Graham · Feb 25, 2014 · Viewed 9.4k times · Source

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.

Answer

Steven picture Steven · Feb 25, 2014

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.