Although There is nothing special about App_Start, but you can make files insinde this folder executes when application started like Application_Start
inside Global.asax
. I am using Ninject dependency injector in my project which has App_Start
folder. There is no Global.asax file in my project:
but all configuration i have set in NinjectWebCommon
file will be executed upon starting application. NinjectWebCommon
has following content:
using WebFormNinject.Models;
[assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")]
namespace WebFormNinject.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
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();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
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.Bind<IDisplay>().To<MockDisplay>();
}
}
}
I was curious where RegisterServices
function will be executed! Then I noticed to this section of code:
[assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")]
These attributes make Start
method executed on application started. For more information look at WebActivator / PreApplicationStartMethod