At Pentia we always try not to change any files that ships in an out-of-the-box Sitecore installation. This makes upgrading easier and the solution code more transparent for developers.
Sitecore ships with a standard Global.asax file and a lot of Sitecore developers choose to make changes directly in this to hook into application and session level events.
For example see the tutorial code from Glass Sitecore Mapper here the Global.asax is used to set the glass context on the Application_Start event.
This could be done in the Sitecore initialize pipeline instead of changing the Global.asax file. It would actually be nice to see the Glass example code moved to the initialize pipeline instead so it fully embraces the Sitecore API :)
I recently worked on a solution Pentia inherited from another Sitecore partner. This solution used the Global.asax to initialize IoC containers through some class on the Application_Start event as shown below.
public void Application_Start()
{
new Initializer().Init();
}
The first thing I did when I saw that code was to move it to where I personally feel it belongs that is in a processor patched into the initialize pipeline. I still dislike their class name, Initializer, but that is something else.
public class InitializeInversionOfControlContainers
{
public void Process(PipelineArgs args)
{
new Initializer().Init();
}
}
Sitecore starts the initialize pipeline on Application_Start so there is nothing that you can do on the Global.asax Application_Start event that you cannot do in a processor in the initialize pipeline.
The same goes for the Application_End event and the shutdown pipeline. Anything you would like to do on Application_End can go in a processor in the shutdown pipeline.
The list below shows which Sitecore pipelines that can be used instead of Global.asax event handlers.
- Application_Start <> initialize
- Application_BeginRequest <> preprocessRequest / httpRequestBegin
- Application_EndRequest <> httpRequestEnd
- Session_End <> sessionEnd
- Application_End <> shutdown
There is no pipeline for Application_Error but Sitecore logs these anyway so I very rarely see a valid reason for hooking into this event.
Why use the standard Sitecore pipelines?
Why not use Global.asax when we all know that it works fine.
It is of course a bit subjective but I prefer to fully embrace the Sitecore API and keep responsibility in one place only. So everything that happens when the application starts can be seen in the initialize pipeline, everything that happens when the application ends can be seen in the shutdown pipeline and so on. Please feel free to share your thoughts on this in the comments.