Lidarr/NzbDrone.Web/Ninject.Web.Mvc/NinjectHttpApplication.cs

104 lines
2.8 KiB
C#
Raw Normal View History

2010-10-12 02:49:27 +00:00
#region License
//
// Authors: Nate Kohari <nate@enkari.com>, Josh Close <narshe@gmail.com>
// Copyright (c) 2007-2009, Enkari, Ltd.
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// See the file LICENSE.txt for details.
//
#endregion
#region Using Directives
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject.Infrastructure;
#endregion
namespace Ninject.Web.Mvc
{
2010-10-18 18:04:07 +00:00
using Ninject.Planning.Bindings.Resolvers;
/// <summary>
2010-10-12 02:49:27 +00:00
/// Defines an <see cref="HttpApplication"/> that is controlled by a Ninject <see cref="IKernel"/>.
/// </summary>
public abstract class NinjectHttpApplication : HttpApplication, IHaveKernel
{
private static IKernel _kernel;
/// <summary>
/// Gets the kernel.
/// </summary>
public IKernel Kernel
{
get { return _kernel; }
}
/// <summary>
/// Starts the application.
/// </summary>
public void Application_Start()
{
lock (this)
{
_kernel = CreateKernel();
2010-10-18 18:04:07 +00:00
_kernel.Components.RemoveAll<IMissingBindingResolver>();
_kernel.Components.Add<IMissingBindingResolver, ControllerMissingBindingResolver>();
_kernel.Components.Add<IMissingBindingResolver, SelfBindingResolver>();
_kernel.Bind<RouteCollection>().ToConstant(RouteTable.Routes);
2010-10-12 02:49:27 +00:00
_kernel.Bind<HttpContext>().ToMethod(ctx => HttpContext.Current).InTransientScope();
_kernel.Bind<HttpContextBase>().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InTransientScope();
2010-10-18 18:04:07 +00:00
_kernel.Bind<IFilterInjector>().To<FilterInjector>().InSingletonScope();
2010-10-12 02:49:27 +00:00
ControllerBuilder.Current.SetControllerFactory(CreateControllerFactory());
_kernel.Inject(this);
OnApplicationStarted();
}
}
/// <summary>
2010-10-18 18:04:07 +00:00
/// Releases the kernel on application end.
2010-10-12 02:49:27 +00:00
/// </summary>
2010-10-18 18:04:07 +00:00
public void Application_End()
2010-10-12 02:49:27 +00:00
{
lock (this)
{
if (_kernel != null)
{
_kernel.Dispose();
_kernel = null;
}
OnApplicationStopped();
}
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
protected abstract IKernel CreateKernel();
/// <summary>
/// Creates the controller factory that is used to create the controllers.
/// </summary>
/// <returns>The created controller factory.</returns>
protected virtual NinjectControllerFactory CreateControllerFactory()
{
return new NinjectControllerFactory(Kernel);
}
/// <summary>
/// Called when the application is started.
/// </summary>
protected virtual void OnApplicationStarted() { }
/// <summary>
/// Called when the application is stopped.
/// </summary>
protected virtual void OnApplicationStopped() { }
}
}