2

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
Para
  • 2,022
  • 5
  • 34
  • 73

3 Answers3

1

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.

David
  • 208,112
  • 36
  • 198
  • 279
0

Be careful with exception handling in Application_Start. In IIS7 Integrated mode you won't have the response object or HTTPContext. See this thread: Request is not available in this context

Community
  • 1
  • 1
Alex
  • 9,250
  • 11
  • 70
  • 81
0

Application_Start is often used to initialize application wide settings which need to be done once per application domain like registering object containers, reading some config initialization values, ... In Session_Start you could place some code which is tied to the specific user who started the session.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928