-
Notifications
You must be signed in to change notification settings - Fork 40
Setting up an IIS hosted web application
This chapter explains how to setup an ASP.NET MVC, WebAPI, ASP.NET WebForms or/and WCF application when it is hosted on IIS. Each of theses web application types require an additional extension which must be installed aside this one. They can all coexist in the same application.
There are two ways to setup a web application. The first one is to use the binaries from the Github download. The second one is to install the nuget package. Both ways will install the same assemblies. The approach described in using binaries from Github can also be used for the NuGet package.
Create a new web application of your choice and add references to Ninject and Ninject.Web.Common from the Github download. Then change the global.asax to derive from NinjectHttpApplication instead of HttpApplication and override CreateKernel to create a kernel and load all modules that you need in your application. One way to load all modules is to tell Ninject to load all modules from your application assemblies. Here is an example of the global.asax in case of an MVC 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();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private void RegisterServices(IKernel kernel)
{
// e.g. kernel.Load(Assembly.GetExecutingAssembly());
}
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
After creating a new web application run Install-Package Ninject.Web.Common.WebHost from the package manager console. Make sure that you are using NuGet 1.6 or higher. Using an older version will result in missing references. This will download and install all required assemblies and add some source code to integrate it into the application. After the installation you will find the NinjectWebCommon class shown below in the App_Start folder.
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
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)
{
// e.g. kernel.Load(Assembly.GetExecutingAssembly());
}
}
Modify the CreateKernel method to create the kernel as you need it in your application. For almost all application no change will be necessary here. Now load your modules or define bindings in RegisterServices method.
Both approaches do exactly the same thing. They hook Ninject into a IIS hosted web application. The only difference is that they use a different approach to do this. The reason for using different approaches 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 of not 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.