The problem is that Application_End seems to be often called while I am browsing my web project
This happens when the AppDomain is unloaded. Although you are debugging it will be every time you recompile your project, which is normal, because every time you recompile the assembly in the bin folder, it is regenerated and ASP.NET just processes the application domain.
When deploying an application to IIS, this will happen less frequently only when IIS decides to redesign the application. This can happen under different circumstances: a certain period of inactivity, CPU / memory thresholds are reached, ...
Should you not use Application_End only at the end of a session or after a certain amount of downtime?
No, Application_End has nothing to do with user sessions. It is called the end of life of the application domain.
Can someone tell me how can I ensure that Application_End is only called when I have really finished using the application?
In this case: Application_End is called by the ASP.NET runtime when the application domain is ready to be unloaded.
So, if you want to avoid this, you should use a persistent database, not storage in memory. If you use storage in memory, then you are tied to the duration of your application, which, as you already noticed, can be very short.
source share