Application_Start Vs Session_Start - asp.net

Actually I am new to ASP.NET and I came through these concepts.
What is the difference between Application_Start Vs Session_Start?
And also how to know how many users are currently active in our application? Is by using Application_Start or by using Session_Start?

Application_Start executes just once when the application/worker process starts or IIS restarts. You can instantiate application variables, for example.
Session_start executes each time a Session is created, such as to instantiate and manage visitor data.

Application_Start Method is Fired on When the Application Started
Application_Start runs once before Files are Processed
Where
Session_Start Method is Fired at the beginning of each session.
Session_Start runs at the starts of each unique user session.

Related

After session timeout ASP.NET not executing Session_Start before invoking new MVC Action

I have some logic as part of Session_Start method in Global.asax.cs file.
the logic loading some information from the database to session variable so all other Controller actions can use them. Everything if working fine until the session expire.
Since i am using Angular (SPA), users actions not goes to the server frequently so the session can just expire after 20 minutes of "server inactivity" (Its not inactivity of the user because he it working on the client side).
Once he want to perform an action that needs to execute a MVC controller action, the controller constructor doesn't find the information on the cache and throws an error.
I expected that the Session_Start will triggered before invoking the controller contractor but the Sesion_Start event not firing at all because i am not assigning information to the session variables, i need only to read them but they are not available.
i tried to assign a dummy data to the session in order to fire the Session_Start event but i can do so only from the controller action but i need the session_start event will be executed BEFORE the Controller constructor.
Is there a way to trigger Session_Start on EVERY server request when the session is not exist (expired)? Is there an option to fire it before the controller constructor?
the problem is that i can not do so from Application_BeginRequest() for example because sessions can not be accessed or use at that scope.
*** This problematic behavior happens only in production. on dev the session_start always fire. i understand its "by design" so it a bit hard to reproduce **

Why Application_Start called two times from different threads?

I can't understand why Application_Start event is called two or three times when my ASP.NET app start? Application_End is not called between Application_Start calls. Also I have only one IIS pool instance.
Check out:
Multiple Application_Start events firing
Does this apply to you?
Also ensure you aren't writing any log entries to the bin folder as that will cause it as well.
Please check the Worker Process Instances. You can find it in the Properties of your Application Pool. This happens if you have more then one instance of worker processes running under the same application pool.

Will Application_End be fired even if …

Application_Start and Application_End are called only once during the lifetime of the application domain – thus they aren’t called for each HttpApplication instance
Application_Start runs when first user requests a page, thus when the first instance of the HttpApplication class is created, while Application_End runs when the last instance of an HttpApplication class is destroyed.
But what if at the time of application domain being restarted there wasn’t any user requests and thus no HttpApplication instances created? Will in that case Application_End still be fired?
Thank you
If I understand your question correctly, you're saying that no requests ever came, if no requests ever come in the application will never be started (i.e. no Application_Start is fired) and therefore no the Application_end wouldn't fire either.
This is not withstanding any kind of "pre-warming" code which fires up the application before requests are received in order to load up caches or things like that.

ASP.NET global.asax usage

When to use and not to use global.asax file in asp.net application? I heard that you should use that file only at a pinch.
The Global.asax file is used to implement application and session level events, such as:
Application_Init - fired when an application first initializes
Application_Start - fired when the application first starts
Application_End - the final event fired when the application ends or times out
Session_Start - fired the first time a user’s session is started
Application_BeginRequest - fired with each new request
Application_EndRequest - fired when the application ends
Application_AuthenticateRequest - the event indicates that a request is ready to be authenticated.
Application_Error - fired when an unhandled error occurs within the application
Session_End - fired whenever a single user Session ends or times out.
Implementing these handlers can all be legitimate uses of the global.asax. For example, the Application_Error event handler typically logs any global errors, and the Application_End event handler typically contains application cleanup logic. These are good uses of the Global.asax. Use them whenever necessary, and don't be afraid if the file grows.
However, I have seen cases where developers have added all sorts of global methods to the global.asax that are indeed un-justified. For example, keep business logic related to a particular domain object inside the object itself rather than in the global.asax. If you find methods in the Global.asax that shouldn't be there refactor the work into the right location.
global.asax is a HTTPModule. All requests go through the global.asax and other modules before they reach your page handlers. Use this to perform certain tasks on your request or response, like url routing, global error handlign etc.
If you need something special to happen on Application start/end or Session start/end, or globally handle exceptions you could use it to map the events in the Apllication and Session life cycles.

When's the earliest i can access some Session data in global.asax?

i want to check if the Session contains some key/value data, in my global.asax. I'm not sure when the earliest possible time (and method name) is, to check this.
thanks :)
I always believed Application_AcquireRequestState was the first event in Global.asax that could access the current session. It's definitely not Application_BeginRequest.
MSDN casually mentions that the session state is acquired during Application_PostAcquireRequestState event. I wish it was restated at the Life Cycle Overview page.
The latest you can access session state is in Application_PostRequestHandlerExecute, as it is saved by SessionStateModule during the next event Application_ReleaseRequestState.
You need to use BeginRequest (http://msdn.microsoft.com/en-us/library/system.web.httpapplication.beginrequest.aspx) as it is the first event fired on the HttpApplication object (which the Global.asax inherits).
You'll see more about the ASP.NET Application Lifecycle here - http://msdn.microsoft.com/en-us/library/ms178473.aspx (this is for IIS 5 & IIS 6).
According to link text, the earliest events in global.asax that you can access session objects is when global.asax fires Session_Start event
Session__Start: Fired when a new user visits the application Web site.
Session__End: Fired when a user's session times out, ends, or they leave the application Web site

Resources