custom application_start code for umbraco website

Rob Gray picture Rob Gray · Feb 29, 2012 · Viewed 9.4k times · Source

I read that for umbraco to run my code on application startup I need to inherit from umbraco.Global and override Application_Start. I've done that with the following simple code which resides in it's own assembly referenced by the umbraco web site project, and in it's bin folder.

public class AtomicF1Global : umbraco.Global
{        
    protected override void Application_Start(object sender, EventArgs e)
    {
        base.Application_Start(sender, e);

        new WindsorStarter().Start();

        throw new Exception("Reached Custom Global");
    }
}

The exception is in there purely to prove to me it wasn't being called.

As far as I know all I need to do is what I've done. I don't need to update an umbraco table anywhere (as is the case when making a number of different modications to umbraco).

However, my code is never being called and I've not been able to find out why. Do I need to register something somewhere?

I've also checked to ensure "App_Global.asax.dll" is not present in the bin directory.

I have also tried creating a Global.asax in the umbraco site project that looks like this:

<%@ Application Language="C#" Inherits="umbraco.Global" %>
<%@ Import Namespace="atomicf1.domain" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Call Global base class first
        base.Application_Start(sender, e);


        // Code that runs on application startup
        new WindsorStarter().Start();

        throw new Exception("Reached Custom Global");

    }             
</script>

Version of umbraco is 4.7.1 (.NET 4.0).

Answer

firepol picture firepol · Jun 17, 2013

I created an empty MVC solution in Visual Studio and added Umbraco 6.x. Edited global.asax.cs and had the same problem as you: function not firing.

The solution seems easy but a bit tricky:

In the global.asax file (if you double click it, visual studio opens global.asax.cs, press "F7" or right click on global.asax and select "View Markup") you have to change the "Inherits" attribute. See original global.asax from umbraco:

<% Application Codebehind="Global.asax.cs" Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>

Change it to this:

<%@ Application Codebehind="Global.asax.cs" Inherits="YourGlobalClassName" Language="C#" %>

Note: if YourGlobalClassName is inside of a Namespace, make sure to include the namespace as well, like this:

<%@ Application Codebehind="Global.asax.cs" Inherits="YourNameSpace.YourGlobalClassName" Language="C#" %>

See also:

Original custom controller documentation from umbraco.

Similar question (addressed to umbraco 6): solution in stackoverflow.