Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alex 35 posts 55 karma points
    Jul 11, 2010 @ 04:40
    Alex
    0

    Accessing global.asax events ?

    I need to perform some initialization on application start up. How can I access global.asax events or something similar?

  • Steen Tøttrup 191 posts 291 karma points c-trib
    Jul 11, 2010 @ 08:53
    Steen Tøttrup
    0

    And you can't just create the global.asax file and put your code in that?

     

  • Alex 35 posts 55 karma points
    Jul 11, 2010 @ 08:56
    Alex
    0

    There is Umbraco's global.asax file alread

  • Steen Tøttrup 191 posts 291 karma points c-trib
    Jul 11, 2010 @ 09:09
    Steen Tøttrup
    0

    There is? Which version of Umbraco? I just look at a few of my 4.0.x sites, and can't seem to find any.

     

  • Matt Brailsford 3960 posts 21147 karma points MVP 9x c-trib
    Jul 11, 2010 @ 09:19
    Matt Brailsford
    2

    Your best bet it to create a class that extends ApplicationBase. These are automatically picked up by umbraco and instantisted on application startup.

    They are mainly used for hooking up events, but there is nothing stopping you from doing other things.

    Matt

  • Steen Tøttrup 191 posts 291 karma points c-trib
    Jul 11, 2010 @ 09:24
    Steen Tøttrup
    0

    Ah Matt, you just beat me to it.

    public class MyGlobal : HttpApplication {

    public MyGlobal() {
    }

    protected void Application_End(object sender, EventArgs e) {
    }

    protected void Application_EndRequest(object sender, EventArgs e) {
    }

    protected void Session_End(object sender, EventArgs e) {
    }
    }

    Make your own class (you can put it in the app_code folder), and just hook-up to the event you need.

    Steen

  • Matt Brailsford 3960 posts 21147 karma points MVP 9x c-trib
    Jul 11, 2010 @ 10:20
    Matt Brailsford
    1

    Hi Steen

    I wouldn't create your own Global class, as Umbraco already does some bits in the app startup, so replacing it would replace the Umbraco functionality.

    I may be wrong, and it may have been updated in v4.5, but my recommendation is to extend ApplicationBase, which is an Umbraco specific class (http://umbraco.org/documentation/books/api-cheatsheet/using-applicationbase-to-register-events) rather than HttpApplication.

    Many thanks

    Matt

  • Dirk De Grave 4539 posts 6017 karma points MVP 3x admin c-trib
    Jul 11, 2010 @ 10:37
    Dirk De Grave
    1

    Yes, agree with Matt, go with a class that inherits from ApplicationBase, and hook up to any event in the ctor...

     

    Cheers,

    /Dirk

  • skiltz 501 posts 701 karma points
    Jul 11, 2010 @ 11:21
    skiltz
    0

    http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/overview-of-all-events

    Which event would you subscribe to in order to run something at application startup?

  • Matt Brailsford 3960 posts 21147 karma points MVP 9x c-trib
    Jul 11, 2010 @ 11:52
    Matt Brailsford
    1

    @skiltz I wouldn't subscribe to any. All classes that inherit ApplicationBase are automatically instantiated at Application startup, so anything in your constructor, is effectivley an Application startup event handler.

    Matt

  • skiltz 501 posts 701 karma points
    Jul 11, 2010 @ 11:57
    skiltz
    0

    ohhh right... that makes sense...

  • Dirk De Grave 4539 posts 6017 karma points MVP 3x admin c-trib
    Jul 11, 2010 @ 12:08
    Dirk De Grave
    0

    @Matt, thanks for explaining it exactly as I was trying to... and was problably misunderstood.

     

    Cheers,

    /Dirk

  • Matt Brailsford 3960 posts 21147 karma points MVP 9x c-trib
    Jul 11, 2010 @ 15:18
    Matt Brailsford
    1

    I've written a blog post with examples just in-case it needed any more explination =)

    http://blog.mattbrailsford.com/2010/07/11/registering-an-application-start-event-handler-in-umbraco/

    Matt

  • Aaron Powell 1708 posts 3045 karma points c-trib
    Jul 12, 2010 @ 12:10
    Aaron Powell
    0

    Why not use a HttpModule? Then you have full control about when it's executed in the request life cycle and you have full access to all the request events.

    Alternatively you can inherit from the umbraco.Global class, there's nothing wrong with doing that, we do it with Snapshot.

  • Matt Brailsford 3960 posts 21147 karma points MVP 9x c-trib
    Jul 12, 2010 @ 12:20
    Matt Brailsford
    0

    Slace, does inheriting umbraco.Global work pre 4.1? I seem to remember trying this a while ago but with no luck.

    Also, wouldn't a httpmodule get invoked every request? I guess you can put logic in to only peform that task once within an application session though.

    Matt

  • Aaron Powell 1708 posts 3045 karma points c-trib
    Jul 12, 2010 @ 12:25
    Aaron Powell
    0

    Oh, I missed that the question was about hooking into application start event. True the module is invoked every request, but if it's just attaching an event handler it wont really be a huge overhead.

    There is a bug in 4.0.x (I don't know if it got resolved at any particular version prior to 4.5) that caused the ApplicationBase types to not always be found.

    Prior to 4.5 you can inherit global, Snapshot is currently only compatible with 4.0.4. You have to remove App_global.asax.dll if it exists and add a global.asax file that points to your class.

  • Matt Brailsford 3960 posts 21147 karma points MVP 9x c-trib
    Jul 12, 2010 @ 12:29
    Matt Brailsford
    0

    Cool, good to know.

    Matt

  • Aaron Powell 1708 posts 3045 karma points c-trib
    Jul 12, 2010 @ 12:49
    Aaron Powell
    0

    4.5 is easier, and the umbraco.Global actually exposes all the events as virtual methods that can be overloaded easily. One of those nice undocumented features ;)

  • shallu 2 posts 22 karma points
    Feb 18, 2013 @ 09:08
    shallu
    0

     

    Hi,

    I am having a probelm accessing Session_start method.

    I am using Umbrco 4.11 with mvc 3 and  my global.asax class looks like this:

     

     

     public class MvcApplication : System.Web.HttpApplication{

     

    //RegisterRoutes && RegisterGlobalFilters methods here

    protected void Session_Start(Object sender, EventArgs e)        {

    //do some stuff heree

             }

    }

    But this method never gets fired.

    Any help will be appreciated.

    Regards

    Shallu

     

     

     

  • Matt Brailsford 3960 posts 21147 karma points MVP 9x c-trib
    Feb 18, 2013 @ 09:10
    Matt Brailsford
    0

    Hi Shallu,

    In 4.11, you'll need to inherit from Umbraco.Web.UmbracoApplication rather than System.Web.HttpApplication as Umbraco needs to register a few things at app startup. You should then be able to override the method you want.

    Cheers

    Matt

  • shallu 2 posts 22 karma points
    Feb 18, 2013 @ 09:26
    shallu
    0

    Hi Matt,

    Thanks for the quick response.

    I just do not seem to be able to override the method.

    It says "no suitable method found to override."

    Niether can i find similar method using VS intelligence.

     

Please Sign in or register to post replies

Write your reply to:

Draft