2013-01-29 09:45:12 +06:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
using System.Web.Hosting;
|
2013-04-25 19:26:06 -10:00
|
|
|
|
using System.Web.Mvc;
|
2013-05-10 10:15:30 -02:00
|
|
|
|
using StackExchange.Profiling;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
2013-01-29 09:45:12 +06:00
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The abstract class for the Umbraco HttpApplication
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This is exposed in the core so that we can have the IApplicationEventHandler in the core project so that
|
|
|
|
|
|
/// IApplicationEventHandler's can fire/execute outside of the web contenxt (i.e. in console applications)
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public abstract class UmbracoApplicationBase : System.Web.HttpApplication
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public static event EventHandler ApplicationStarting;
|
|
|
|
|
|
public static event EventHandler ApplicationStarted;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Boots up the Umbraco application
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal void StartApplication(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2013-04-25 19:26:06 -10:00
|
|
|
|
//don't output the MVC version header (security)
|
|
|
|
|
|
MvcHandler.DisableMvcResponseHeader = true;
|
|
|
|
|
|
|
2013-01-29 09:45:12 +06:00
|
|
|
|
//boot up the application
|
|
|
|
|
|
GetBootManager()
|
|
|
|
|
|
.Initialize()
|
|
|
|
|
|
.Startup(appContext => OnApplicationStarting(sender, e))
|
|
|
|
|
|
.Complete(appContext => OnApplicationStarted(sender, e));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes the Umbraco application
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
protected void Application_Start(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
StartApplication(sender, e);
|
2013-05-12 19:05:49 -10:00
|
|
|
|
|
|
|
|
|
|
if (SystemUtilities.GetCurrentTrustLevel() < AspNetHostingPermissionLevel.High)
|
|
|
|
|
|
{
|
|
|
|
|
|
//If we don't have a high enough trust level we cannot bind to the events
|
|
|
|
|
|
LogHelper.Info<UmbracoApplicationBase>("Cannot use the MiniProfiler since the application is running in Medium trust");
|
|
|
|
|
|
}
|
2013-01-29 09:45:12 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-12 15:25:46 -10:00
|
|
|
|
/// <summary>
|
2013-05-12 19:05:49 -10:00
|
|
|
|
/// Initializes the mini profiler for the request
|
2013-05-12 15:25:46 -10:00
|
|
|
|
/// </summary>
|
2013-05-12 19:05:49 -10:00
|
|
|
|
protected void Application_BeginRequest()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (GlobalSettings.DebugMode == false) return;
|
|
|
|
|
|
var request = TryGetRequest();
|
|
|
|
|
|
if (request.Success == false) return;
|
|
|
|
|
|
if (request.Result.Url.IsClientSideRequest()) return;
|
|
|
|
|
|
if (SystemUtilities.GetCurrentTrustLevel() < AspNetHostingPermissionLevel.High) return;
|
|
|
|
|
|
if (string.IsNullOrEmpty(request.Result["umbDebug"]) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
//start the profiler
|
|
|
|
|
|
MiniProfiler.Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Closes the mini profiler for the request
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected void Application_EndRequest()
|
2013-05-10 10:15:30 -02:00
|
|
|
|
{
|
2013-05-12 19:05:49 -10:00
|
|
|
|
if (GlobalSettings.DebugMode == false) return;
|
|
|
|
|
|
var request = TryGetRequest();
|
|
|
|
|
|
if (request.Success == false) return;
|
|
|
|
|
|
if (request.Result.Url.IsClientSideRequest()) return;
|
|
|
|
|
|
if (SystemUtilities.GetCurrentTrustLevel() < AspNetHostingPermissionLevel.High) return;
|
|
|
|
|
|
if (string.IsNullOrEmpty(request.Result["umbDebug"]) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
//stop the profiler
|
|
|
|
|
|
MiniProfiler.Stop();
|
|
|
|
|
|
}
|
2013-05-10 10:15:30 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-12 19:05:49 -10:00
|
|
|
|
|
2013-01-29 09:45:12 +06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Developers can override this method to modify objects on startup
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
protected virtual void OnApplicationStarting(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ApplicationStarting != null)
|
|
|
|
|
|
ApplicationStarting(sender, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Developers can override this method to do anything they need to do once the application startup routine is completed.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
protected virtual void OnApplicationStarted(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ApplicationStarted != null)
|
|
|
|
|
|
ApplicationStarted(sender, e);
|
|
|
|
|
|
}
|
2013-05-12 19:05:49 -10:00
|
|
|
|
|
2013-01-29 09:45:12 +06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A method that can be overridden to invoke code when the application has an error.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
protected virtual void OnApplicationError(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void Application_Error(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Code that runs when an unhandled error occurs
|
|
|
|
|
|
|
|
|
|
|
|
// Get the exception object.
|
|
|
|
|
|
var exc = Server.GetLastError();
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore HTTP errors
|
|
|
|
|
|
if (exc.GetType() == typeof(HttpException))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LogHelper.Error<UmbracoApplicationBase>("An unhandled exception occurred", exc);
|
|
|
|
|
|
|
|
|
|
|
|
OnApplicationError(sender, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A method that can be overridden to invoke code when the application shuts down.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
protected virtual void OnApplicationEnd(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void Application_End(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SystemUtilities.GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Info<UmbracoApplicationBase>("Application shutdown. Reason: " + HostingEnvironment.ShutdownReason);
|
|
|
|
|
|
}
|
|
|
|
|
|
OnApplicationEnd(sender, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract IBootManager GetBootManager();
|
|
|
|
|
|
|
2013-05-12 19:05:49 -10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the request object from the app instance if it is available
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private Attempt<HttpRequestBase> TryGetRequest()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var req = Request;
|
|
|
|
|
|
return new Attempt<HttpRequestBase>(true, new HttpRequestWrapper(req));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Attempt<HttpRequestBase>(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-01-29 09:45:12 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|