Home / .NET / Ninject.MVC3 and Ninject.Web.MVC3 merged to one package

Ninject.MVC3 and Ninject.Web.MVC3 merged to one package



With Ninject.MVC3 2.2.0.3 available from NuGet Ninject.MVC3 and Ninject.Web.MVC3 got merged into one project providing the advantage or both.

A long time ago Microsoft released with Ninject.MVC3 a DependencyResolver for Ninject in the NuGet gallery. A lot of people started using Ninject.MVC3 because it was easily installable by NuGet. Unfortunately, this package does not provide the full power of the official implementation Ninject.Web.MVC3. Therefore, we decided to replace this package with an implementation using Ninject.Web.MVC3 while still using the WebActivator to integrate the bootstapper into the application.

What changed for Ninject.MVC3 users?

In order to make that the package correctly disposes the Ninject kernel on application shutdown the bootstrapper code has been moved into NinjectHttpApplicationModule.cs. This means that you have to specify your modules and bindings in this file now and not in AppStart_NinjectMVC3.cs anymore. Let’s have a look at this bootstrapper:

namespace MvcApplication2
{
    using System;
    using System.Web;

    using Ninject;
    using Ninject.Web.Mvc;

    public sealed class NinjectHttpApplicationModule : IHttpModule, IDisposable
    {
        #region Ninject Mvc3 extension bootstrapper (Do not touch this code)
        [...]
        #endregion

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
        }
    }
}

As you see there are two methods now that can be changed to your needs. In CreateKernel the Ninject Kernel is created. The default implementation (create a standard kernel) fits for almost every application. In RegisterServices you can load your modules or define bindings. I have removed the bootstrapping section from the code above intentionaly. I’ll explain this code later in this blog post.

Which new features were added compared to Ninject.MVC3?

The new package adds two very important features:

  • The Ninject core and all scoped objects are properly disposed at shut down of the application.
  • Objects that are InRequestScope are released immediately after the request ends. This makes sure that applications that use a unit of work pattern and have a unit of work pattern do not run into out of memory exceptions.

Additionally, the package brings all the features that were added to Ninject.Web.Mvc. I blogged about these features several week ago.

What changed for Ninject.Web.MVC3 users?

If you get the binaries from github or the build server or if you build the sources yourself then absolutely nothing changed. But if you decide to thake advantage of Nuget and install Ninject.MVC3 from there you have to change one or two things in your application:

  1. The bootstrapper does not derive from HttpApplication anymore. Therefore you have to move your kernel configuration to NinjectHttpApplicationModule.cs as explained in chapter one.
  2. If you had some code in OnApplicationStarted or OnApplicationStopped you have to move this code to global.asax.cs. Just add two new methods void Application_Start() and Application_End()

What is the bootstrapper doing in detail?

The bootstrapping has been extracted to a module. As you see in the code below it ensures that initialization and disposing happens exactly once.  Both actions are forwarded to the bootstrapper that comes with Ninject.Web.Mvc. This newly introduced bootstrapper is exactly the same as NinjectHttpApplication except that it is not derived from HttpApplication anymore so that it can be used in other ways such as here in the bootstrapper module. In fact, the new NinjectHttpApplication implementation uses the bootstrapper internally itself now to get everything running.

/// <summary>
/// Initializes a module and prepares it to handle requests.
/// Do not change this method!
/// </summary>
/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
public void Init(HttpApplication context)
{
    lock (bootstrapper)
    {
        if (initialized)
        {
            return;
        }

        initialized = true;
        bootstrapper.Initialize(CreateKernel);
    }
}

/// <summary>
/// Disposes the <see cref="T:System.Web.HttpApplication"/> instance.
/// Do not change this method!
/// </summary>
public void Dispose()
{
    lock (bootstrapper)
    {
        if (kernelDisposed)
        {
            return;
        }

        kernelDisposed = true;
        bootstrapper.ShutDown();
    }
}