Search code examples
asp.netglobal-asaxapplication-start

What code whould be placed in the application_start and the session_start events in global.asax?


Can you give an example of what might be best suited to place in the Application_Start and Session_Start subroutines? I know when each subroutine is called. Application_Start when the first user first accesses the web application. Session_Start when a user opens a session with the application. But what code belongs in each of these subroutines. What should the code in each subroutine do?


Solution

  • Just any examples? Well, in an MVC site the routes are registered in Application_Start. That's also a good place to initialize an IoC container such as StructureMap. Maybe initialize some singletons you have in your application if you want them to be readily available rather than late-bound when a user accesses them (like if they have a high initialization cost and you'd rather do one slow initial hit on the website yourself than bother a customer or two with it).

    Session_Start is generally used less often, but could be a good place for per-user (or per-session, realistically) tracking of some kind.