using System; using System.Linq; using System.Web; using System.Web.Hosting; using System.Web.Mvc; using StackExchange.Profiling; using Umbraco.Core.Configuration; using Umbraco.Core.LightInject; using Umbraco.Core.Logging; using Umbraco.Core.ObjectResolution; using Umbraco.Core.Logging; namespace Umbraco.Core { /// /// The abstract class for the Umbraco HttpApplication /// /// /// 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) /// public abstract class UmbracoApplicationBase : System.Web.HttpApplication { /// /// Umbraco application's IoC container /// internal ServiceContainer Container { get; private set; } private ILogger _logger; /// /// Constructor /// protected UmbracoApplicationBase() { //create the container for the application, the boot managers are responsible for registrations Container = new ServiceContainer(); } public event EventHandler ApplicationStarting; public event EventHandler ApplicationStarted; /// /// Called when the HttpApplication.Init() is fired, allows developers to subscribe to the HttpApplication events /// /// /// Needs to be static otherwise null refs occur - though I don't know why /// public static event EventHandler ApplicationInit; public static event EventHandler ApplicationError; public static event EventHandler ApplicationEnd; /// /// Boots up the Umbraco application /// internal void StartApplication(object sender, EventArgs e) { //boot up the application GetBootManager() .Initialize() .Startup(appContext => OnApplicationStarting(sender, e)) .Complete(appContext => OnApplicationStarted(sender, e)); } /// /// Initializes the Umbraco application /// /// /// protected void Application_Start(object sender, EventArgs e) { StartApplication(sender, e); } /// /// Override init and raise the event /// public override void Init() { base.Init(); OnApplicationInit(this, new EventArgs()); } /// /// Developers can override this method to modify objects on startup /// /// /// protected virtual void OnApplicationStarting(object sender, EventArgs e) { if (ApplicationStarting != null) ApplicationStarting(sender, e); } /// /// Developers can override this method to do anything they need to do once the application startup routine is completed. /// /// /// protected virtual void OnApplicationStarted(object sender, EventArgs e) { if (ApplicationStarted != null) ApplicationStarted(sender, e); } /// /// Called to raise the ApplicationInit event /// /// /// private void OnApplicationInit(object sender, EventArgs e) { if (ApplicationInit != null) ApplicationInit(sender, e); } /// /// A method that can be overridden to invoke code when the application has an error. /// /// /// protected virtual void OnApplicationError(object sender, EventArgs e) { var handler = ApplicationError; if (handler != null) handler(this, EventArgs.Empty); } 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; } Logger.Error("An unhandled exception occurred", exc); OnApplicationError(sender, e); } /// /// A method that can be overridden to invoke code when the application shuts down. /// /// /// protected virtual void OnApplicationEnd(object sender, EventArgs e) { var handler = ApplicationEnd; if (handler != null) handler(this, EventArgs.Empty); } protected void Application_End(object sender, EventArgs e) { if (SystemUtilities.GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted) { Logger.Info("Application shutdown. Reason: " + HostingEnvironment.ShutdownReason); } OnApplicationEnd(sender, e); } protected abstract IBootManager GetBootManager(); /// /// Returns the logger instance for the application - this will be used throughout the entire app /// public virtual ILogger Logger { get { return _logger ?? (_logger = Logging.Logger.CreateWithDefaultLog4NetConfiguration()); } } } }