﻿using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using LightInject;
using StackExchange.Profiling;

namespace nH.Web
{
	public class MvcApplication : HttpApplication
	{
		private ServiceContainer _container;

		protected void Application_Start()
		{
			_container = new ServiceContainer();

			AreaRegistration.RegisterAllAreas();

			GlobalConfiguration.Configure(WebApiConfig.Register);
			FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
			RouteConfig.RegisterRoutes(RouteTable.Routes);
			DiConfig.Register(_container, GlobalConfiguration.Configuration);
		}

		protected void Application_End()
		{
			_container.Dispose();
		}

		protected void Application_BeginRequest()
		{
			if (Request.IsLocal)
			{
				MiniProfiler.Start();
			}
		}

		protected void Application_EndRequest()
		{
			MiniProfiler.Stop();
		}
	}
}