Reputation: 42374
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:
NinjectHttpApplication
in Global.asax.csNinjectModule
in the RegisterServices
method in NinjectMVC3.csAnd 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();
}
}
}
Upvotes: 2
Views: 3947
Reputation: 24236
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.
Upvotes: 5