Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom application_start code for umbraco website

Tags:

c#

.net

umbraco

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).

like image 969
Rob Gray Avatar asked Feb 28 '12 23:02

Rob Gray


4 Answers

Implementing your own global.asax is the correct way as it will run upon application start.

I have implemented my global.asax in a similar way to yours, however I place the code in a separate CS file in a separate project - but then that's just my way.

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

        XmlConfigurator.Configure();
        ILog log = LogManager.GetLogger(typeof (Global));
        log.Debug("Application started");
    }

}

Looking at the differences, you would need to add protected override to your method.

Personally, I always add logging (using log4net) to my global.cs file so I can ensure that my logging is up and running and that the application has restarted.

If your code is not being picked up, you may want to try amending the web.config to force a application restart, but also step through with the debugger to make sure that your code just isn't being reached for some other reason.

like image 35
Digbyswift Avatar answered Oct 22 '22 23:10

Digbyswift


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.

like image 133
firepol Avatar answered Oct 22 '22 23:10

firepol


If you want code to run on application start up, the way you'd normally do it is to create a class that inherits from ApplicationBase. Public classes that inherit from this are called when the Umbraco application starts up. Here's some example code from one of the startup events that we use on one of our Umbraco sites:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using umbraco.BasePages;

namespace MySite.Events
{
    public class EventHandlers : ApplicationBase
    {
        public EventHandlers()
        {
            //put your code to run on app startup here
        }
    }
}
like image 1
Tim Avatar answered Oct 22 '22 23:10

Tim


I would use the WebActivator package. Then you can make your code run without bothering with global.asax and overriding Umbraco methods.

See this blog post by Bart Wullems for a quick start.

like image 1
azzlack Avatar answered Oct 22 '22 23:10

azzlack