Ninject + MVC3 = InvalidOperationException: Sequence contains no elements

I created a new MVC3 project, hit F5, saw the sample page.

Then I used NuGet to get the Ninject.MVC extension. I modified my global.asax according to the Ninject documentation, How To Setup an MVC3 Application:

public class MvcApplication : NinjectHttpApplication
{
   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 });
   }

   protected override IKernel CreateKernel()
   {
       var kernel = new StandardKernel();
       kernel.Load(Assembly.GetExecutingAssembly());
       return kernel;
   }

   protected override void OnApplicationStarted()
   {
       base.OnApplicationStarted();

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

Now when I run the app, I get the yellow screen of death with the following exception:

InvalidOperationException - Sequence contains no elements.

at System.Linq.Enumerable.Single(...)

at Ninject.Web.Mvc.Bootstrapper.Initialize(...) line 67.

And sure enough, line 67 of that file calls .Single(), thus throwing the exception.

What am I doing wrong?


I have to add to this in the hopes that someone else will resolve the issue more quickly and not want to pull out every strand of hair on their head like I almost did.

I needed to rename everything in my project to match new business terms. I changed namespaces everywhere and I even changed the Assembly Name (right-click project > properties > application tab) so that the generated assembly matches the new naming convention. The assembly rename is what made Ninject very angry!

By renaming the assembly that gets generated a new file with the new name was being created when we compiled. However, the old file with the old name was still in the bin directory! If you have Ninject activating via the added class in App_Start then this activation class will get invoked in BOTH assemblies (old one AND new renamed one). Don't ask me how or why, but it does and it gives you this "already initialized" error.

Not even cleaning solution works because Visual Studio will only remove the binaries that it is generating, which would be the new renamed ones. It leaves the old ones alone just sitting there.

Go delete your bin folder before you try doing anything else! I hope this saves someone else from wasting valuable working hours!


You might notice that after installing the ninject.mvc3 NuGet there is an App_Start subfolder created inside your project containing an NinjectMVC3.cs file. Delete this folder and try again. So here are the steps I followed:

  1. Create a new ASP.NET MVC 3 project using the default template
  2. Bring up the Package Manager Console window (View -> Other Windows -> Package Manager Console)
  3. Type install-package ninject.mvc3 on the command line
  4. Replace the default code in Global.asax with the code in your question
  5. Delete the AppStart subfolder created during the installation of the package
  6. Run the application
  7. Enjoy the beauty of the /Home/Index default page opened in your Google Chrome web browser :-)

I have updated the documentation Wiki linked in your question to show both ways to setup a MVC3 application. I suggest to use the second option which is the prefered way for theNuGetpackage.

Instead of deriving from NinjectHttpApplication it is using the NinjectMVC.cs in the AppStart folder which is created during installation of the package. This is also the location where you create the kernel and where you load your modules or where you define the bindings.