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.
@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.
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.
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.
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.
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 ;)
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.
Accessing global.asax events ?
I need to perform some initialization on application start up. How can I access global.asax events or something similar?
And you can't just create the global.asax file and put your code in that?
There is Umbraco's global.asax file alread
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.
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
Ah Matt, you just beat me to it.
Make your own class (you can put it in the app_code folder), and just hook-up to the event you need.
Steen
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
Yes, agree with Matt, go with a class that inherits from ApplicationBase, and hook up to any event in the ctor...
Cheers,
/Dirk
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?
@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
ohhh right... that makes sense...
@Matt, thanks for explaining it exactly as I was trying to... and was problably misunderstood.
Cheers,
/Dirk
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
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.
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
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.
Cool, good to know.
Matt
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 ;)
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
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
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.
is working on a reply...