Ninject MVC3 - bootstrapper throwing "Already Initialized" exception

Yngve B-Nilsen picture Yngve B-Nilsen · May 6, 2011 · Viewed 11k times · Source

I've created an empty Asp.Net MVC3 project, and used nuget install-package Ninject.MVC3

Without doing anything else (no services registered and not even a controller created) I run the application.

The Project breaks on line 22 in NinjectMVC3.cs with the following exception:

[InvalidOperationException: Already Initialized!] Ninject.Web.Mvc.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Maintenance2.2\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\Bootstrapper.cs> :58 Mvc.App_Start.NinjectMVC3.Start() in c:\Projects\Events\Events\App_Start\NinjectMVC3.cs:22

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +0
System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +72
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +335
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +28
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +19
WebActivator.BaseActivationMethodAttribute.InvokeMethod() +199 WebActivator.ActivationManager.RunActivationMethods() +330 WebActivator.ActivationManager.RunPreStartMethods() +27 WebActivator.ActivationManager.Run() +39

The line in NinjectMVC3.cs is:

public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
            DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
            bootstrapper.Initialize(CreateKernel); // <- this one right here...
        }

What is going on with this? I've used Ninject for MVC3 in other projects without problems at all. I do notice that the second line in the Start() method is not included in older (weeks) packages of Ninject.MVC3, so maybe something else has been broken in a recent update also?

Please help!

Edit to address answer from Remo

This is from my global.asax.cs

 public class MvcApplication : System.Web.HttpApplication
 {

The global.asax is 100% standard aswell. No adjustments done.

EDIT - Problem is gone...

I honestly have no idea what caused this, but after a few restarts of Visual Studio, and a full reboot, the project is working as it should. Neither can I recreate the problem in new projects.

Sorry for wasting your time :)

Answer

Andrew picture Andrew · Jun 3, 2011

Take a look at Setting up a MVC3-application

In short:

You probably have een App_Start folder in your project with a NinjectMVC3.cs file.

When using the NuGet package in combination with modifying the Global.asax (for which there is no reason) you have to remove the App_Start folder and remove the references to WebActivator and Microsoft.Web.Infrastructure