2

I just added the Ninject.MVC3 nuget package (v2.2.2.0) to my ASP.NET MVC 3 app.

I seem to have set up my ASP.NET MVC 3 app in two different ways:

  1. By inheriting from NinjectHttpApplication in Global.asax.cs
  2. By loading a NinjectModule in the RegisterServices method in NinjectMVC3.cs

And now I'm trying to make sense of this blog entry, referenced here. It seems to be saying there's yet another method. Something about NinjectHttpApplicationModule. I'm lost.

How should I modify my NinjectMVC.cs and Global.asax.cs files? What I have now is pasted below.


NinjectMVC3.cs

[assembly: WebActivator.PreApplicationStartMethod(typeof(TennisClub.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(TennisClub.App_Start.NinjectMVC3), "Stop")]

namespace MyApp.App_Start
{
    using System.Reflection;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Mvc;
    using TennisClub.Configuration;

    public static class NinjectMVC3 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
            DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <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)
        {
            kernel.Load(new MainModule()); // I added this
        }        
    }
}

Global.asax

namespace MyApp
{
    public class MvcApplication : NinjectHttpApplication // new
    {
        protected override void OnApplicationStarted() // new
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            ModelMetadataProviders.Current = new CustomModelMetadataProvider();
            ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
            DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
        }

        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 = Token.Home, action = Token.Index, id = UrlParameter.Optional }); // Parameter defaults
        }

        protected override IKernel CreateKernel() // new
        {
            return new StandardKernel(new MainModule());
        }
    }
}

MainModule.cs

namespace MyApp.Configuration
{
    public class MainModule : NinjectModule
    {
        public override void Load()
        {
            Bind<AppSettings>().ToSelf().InSingletonScope();
            Bind<MainDbContext>().ToSelf().InRequestScope();
            Bind<HttpContext>().ToMethod(context => HttpContext.Current);
            Bind<UserInfo>().ToSelf().InRequestScope();
        }
    }
}
Community
  • 1
  • 1
devuxer
  • 41,681
  • 47
  • 180
  • 292

1 Answers1

5

Have a look at this page from the Wiki on GitHub -

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

It goes through both the different approaches and has helped me in the past.

ipr101
  • 24,096
  • 8
  • 59
  • 61
  • Okay, so it sounds like you are saying I should just revert my Global.asax to its original form and use the `App_Start.NinjectMVC3` class. But what is `NinjectHttpApplicationModule`? – devuxer Oct 04 '11 at 08:40
  • Looks like the page you referenced is more recent than the blog post (which is where I was getting confused about `NinjectHttpApplicationModule`), so I guess I'm going to try that for now. I have some other things to figure out before I can test whether it works, but +1 for your help. – devuxer Oct 04 '11 at 08:48