Parameter not registered when using Simple Injector to enter MVC controller constructor

I have what seems like a very simple problem when using Simple Injector to embed dependencies in an MVC controller. I am new to using Simple Injector, which previously used StructureMap.

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

The error I get when viewing / Index view of HomeController:

The constructor of the HomeController type contains an IContext parameter named "context", which is not registered. Make sure IContext is registered in the container or modify the constructor of the HomeController.

The controller looks like this:

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

IContext is just a simple interface:

 public interface IContext { } 

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

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

For information, the IContext interface lives in another VS / Assembly project for implementing DbContext. They refer to the MVC project.

My Global.asax.cs file has the following:

 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 does not work, since all the code follows the instructions for starting Simple Injector quickly.

+6
source share
1 answer

Make sure that the IContext referenced by the HomeController is actually the same type as the IContext that you registered with Application_Start . This is most likely a different type. Performing the โ€œgoto definitionโ€ in Visual Studio on both the IContext in the registration and the IContext in the HomeController allows you to confirm that Visual Studio is IContext to the same file.

One more thing to check: does the Container instance indicated in your code show the actual (and only) container that is registered in MVC. Search for any other new Container registrations in your application.

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

+6
source

All Articles