How to get the last time the current ASP.NET app was initiated?
If you wish to forego an extra variable, I think this will give you the last time the IIS worker process (a.k.a. application pool) was restarted:
System.Diagnostics.Process.GetCurrentProcess().StartTime
I use it to set cacheability because the page is based on content that I only dynamically generate on application startup:
Response.Cache.SetLastModified(System.Diagnostics.Process.GetCurrentProcess().StartTime);
It is possible to stop/start individual websites within an application pool / worker process, but I infer from this post that doing so will not recreate static application objects, so I gather that the date associated with the worker process is probably the most useful date here.
Keep in mind also that a static application variable created on application startup will actually often times give you the time the application was first visited. It is possible the worker process was started much earlier, and that brings into mind phrases like "IIS application warm up" and "IIS application autostart" and another discussion on when static fields are initialized in general. What you choose might depend on whether or not you're interested in the last worker process recycle time or if you're interested in the time other static members were computed.
You can set a static field to DateTime.Now
in Application_Start
in Global.asax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With