Ninject MVC3 - bootstrapper throwing "Already Initialized" exception
Asked Answered
D

15

35

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

Delete answered 6/5, 2011 at 10:57 Comment(2)
not quite sure what to do with this question, since there probably is no correct answer.. Do I flag it, close it or something?Delete
You can either select the answer that helped the most, or answer it yourself with details about what you did (essentially, your edit). Seems odd, but that's the way this is normally handled.Unicorn
D
3

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.

Delete answered 8/5, 2011 at 11:58 Comment(1)
FYI, I also got this error when I had done a bunch of renames in my project and I didn't do a "Clean Solution" (Actually I did, but it didn't work the first time). The result was old DLL's still living in the bin directory that were somehow initializing Ninject too!Contestant
E
34

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

Epaminondas answered 3/6, 2011 at 12:44 Comment(2)
I was about to delete the NuGet files but this worked like a charm.Rawdan
Wow! What a frustrating experience all just to get Ninject to work!Hubie
L
16

This situation came up for me when i changed the namespaces of my project, then switched it back again.

even when i cleaned the solution and did an iisreset it came back with 'already initialized'.

the problem arose from the dlls that had been created in the bin directory when i changed the namespace and built the project - after changing the namespaces back and cleaning the solution the dlls from the alternative namespace were still there, and hence were being loaded when the web application started up, causing the 'already initialized' error.

deleting the dlls fixed the issue.

Landsturm answered 16/7, 2011 at 17:17 Comment(1)
THIS! I just spent two hours trying to figure out what the heck I did. I renamed all my namespaces, including the named binaries that get built and then suddenly ninject started screaming. I ended up with the old binaries in the bin folder and the new ones. Both webactivators for ninject were getting called. Clean solution doesn't work because it will only clean up the binaries it's generating and since I renamed everything it didn't consider those old binaries to be of any concern. GO DELETE YOUR BIN AND OBJ FOLDERS NOW AND THEN GO CELEBRATE WITH PIZZA AND BEER.Mcgrew
F
12

Most likely you are deriving from NinjectHttpApplication and using AppStart at the same time. These two ways shouldn't be mixed up.

Read the documentation https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application

Frig answered 6/5, 2011 at 11:59 Comment(4)
please see my update.. I'm not actually... Provided you are referring to the 'old' way of using Ninject with MVC where you derived the MvcApplication in global.asax.cs?Delete
Hmm strange. I just checked and I have no problem. This exception is thrown in case bootstrapper.Initialize(CreateKernel) is called twice. You you might want to debug if the Start method is executed twice.Frig
Can you expand a little more please. Give a correct code sample would be ace.Alyse
I already updated th Doc monts ago. Read github.com/ninject/ninject.web.mvc/wiki/…Frig
C
5

I recently ran into the same problem. In my case, I have multiple projects in my solution... and more that one of those projects contains NuGet package references to Ninject.MVC3.

When I updated all of my Ninject package versions, App_Start folders were created on all of the projects that have NuGet references. So, I had to go and delete the App_Start folders from the class library projects; just leaving the App_Start folder on the main MVC application.

Circuitry answered 20/8, 2012 at 21:1 Comment(1)
Yes, look out for unwanted App_Start folders in class library project references!Dacha
D
3

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.

Delete answered 8/5, 2011 at 11:58 Comment(1)
FYI, I also got this error when I had done a bunch of renames in my project and I didn't do a "Clean Solution" (Actually I did, but it didn't work the first time). The result was old DLL's still living in the bin directory that were somehow initializing Ninject too!Contestant
F
3

I'm not sure what it was that was causing your application to bomb, but I'm glad doing VS restarts solved it. I had the same error happening but was unable to correct it no matter how many clean/rebuilds I did or how many times I restarted VS.

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.

I hope this helps someone else, I wasted a few hours on it when all I needed was to dump the NuGet Package.

Ferrara answered 18/5, 2011 at 15:59 Comment(0)
S
2

read the docs!

https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application

What's the difference between these approches?

Both approaches exactly do the same. They hook Ninject into MVC applications. The only difference is that they use a different approch to do this. The reason for using different approchaches is that the NuGet package can provide out of the box integration by adding one file. Otherwise it would require complicated modifications of the global.asax

NOTE: If you decide to go with the first approach if using the NuGet package (for which there is no reason) you have to delete the App_Start folder and remove the references to WebActivator and Microsoft.Web.Infrastructure.

Sivia answered 21/7, 2011 at 10:44 Comment(0)
A
2

I had the same problem when I upgraded the Ninject.Web.MVC3 package to version 2.2.2.0. I noticed that this update added the following line to my .csproj file (VCS FTW!):

<Compile Include="App_Start\NinjectMVC3.cs" />

Changing the Build action from Compile to Content for that file solved my problem. App_Start is as evil as App_Code after all...

Allie answered 1/8, 2011 at 9:19 Comment(0)
T
2

I solved this issue by manually removing some dlls in the /bin folder, which wasn't being removed when running the Cleanup command from Visual studio.

Tuckerbag answered 6/10, 2011 at 9:4 Comment(0)
M
1

I had the same issue with some slight differences I was inheriting from NinjectHttpApplication and overriding OnApplicationStarted. 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.

Malayopolynesian answered 16/5, 2011 at 23:10 Comment(0)
L
1

i spent about 4 hours messing around with the nuget packages, to no avail.

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.

this is what my code looks like, but I think there may also be an issue in the nuget packages...

public class MvcApplication : NinjectHttpApplication
{
    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();

        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    protected override Ninject.IKernel CreateKernel()
    {
        // declaratively add the Ninject Modules that we have built to manage our dependencies...
        var modules = new INinjectModule[]{
            new RepoModule()
        };

        return new StandardKernel(modules);
    }

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }
}
Lalittah answered 19/5, 2011 at 4:20 Comment(0)
A
1

make sure only one assembly have the "NinjectMVC3" class ,if a.dll,b.dll all have the NinjectMVC3 class in then bin directory ,There will be such a mistake!

Adriaadriaens answered 26/7, 2011 at 6:49 Comment(0)
Q
1

I just had this same issue and I realized that it was because NuGet added NinjectMVC3.cs files to both my web app and my DAL library. Didn't realize that had occurred, but once I deleted the file from the DAL library it was fine.

Questa answered 3/1, 2012 at 1:15 Comment(0)
B
0

When you use nuGet there is a folder App_start being created, take a look into it.

Balmuth answered 10/7, 2011 at 18:15 Comment(0)
B
0

I'll add this as an answer only because I've not seen anyone mention it yet and it is worth checking before going off and cloning repos and whatnot.

I experienced this problem out of the blue on a solution which contains maybe 15 projects, three of which are websites. I had been working on one of the sites when I started getting this problem without me having changed anything that would obviously cause the problem: I had not just added Ninject anywhere (it was already installed and working just fine), the global asax files were not deriving from the Ninject classes and nothing was amiss in any App_Start folder.

After quite a bit of messing about with NuGet and the like to no avail (brought on by messages about unresolved references - seemed like Ninject was not being referenced properly but it was), i went through all the files I had changed paying particular attention to project and config files, and discovered that somehow Visual Studio had added one of the other websites as a project reference to the one I was trying to run. As a result the Ninject config was being run twice and causing the problem, so the root cause was the same but the method of screw-up was different.

There was no reason for that project reference, it was not there before as it wasn't a valid dependency, I did not add it manually and I still have no idea how it happened but removing it immediately fixed the problem.

Beadruby answered 6/9, 2016 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.