Application_End is called too early / often

I am using a temporary database in a project that is located on Application_End :

 protected void Application_End() { if (_db != null) _db.Dispose(); } 

The problem is that Application_End seems to be called often while viewing my web project - it seems that when I edit the object in db this change is successful, the database is deleted, and by the time I am redirected to index - a new db was created and shows an immutable object, as if nothing had happened.

Should Application_End be called only at the end of a session or after a certain amount of downtime?

Can someone tell me how I can make sure that Application_End is only called when I really finished using the application?

+4
source share
1 answer

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.

+10
source

Source: https://habr.com/ru/post/1392639/


All Articles