Search code examples
c#.netasp.net-mvc-3ninject

Ninject MVC3 - bootstrapper throwing "Already Initialized" exception


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 :)


Solution

  • 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 :)

    UPDATE

    Since posting this answer a few good answers has appeared. I'm updating this answer to collect the different approaches in one answer. Hope that's ok for you guys:

    DevilDog74 answered

    finally i went to gitub and cloned a new repo, downloaded the latest Ninject.Web.Common and latest Ninject2 release builds and did a local release build. then I ditched the nuget packages and made assembly references to the newly compiled > assemblies "Ninject", "Ninject.Web.Common" and "Ninject.Web.Mvc" and my project started > working and controllers were being created with their dependencies being resolved.

    Jeff Circeo answered

    I resolved the issue by downloading from https://github.com/ninject/ninject.web.mvc instead of using the nuget package. I didn't have to do anything after I added the references.

    Marcus King answered

    What ended up working for me was to remove the Ninject MVC NuGet Package completely and just add the Ninject and Ninject dll's the old fashioned way. I think there may be something wrong with their NuGet package.