I am writing a program recording service calls and treatment done. We have a number of users who open and close calls and I want to show at all times the total number of calls opened today and the total number closed today and the difference between them. I thought of doing it with an application variable. I have to reset these variables to 0 every day. Where would I do that? I thought in the Global.asax but in which event could that be done? The application is running all the time so I suppose Application_Start wouldn't be appropriate. So where? Thank you.
I would store the calls to a database and do a select which groups by the current day to get the total calls, etc. for display.
That way it will automatically reset for you when a new day starts, and you don't need to worry about IIS Resets destroying your in memory data.
If you don't want the performance hit of querying too often, there are a number of caching options available.
You could configure the Periodic Restart Settings for Application Pool Recycling in IIS:
The element contains configuration settings that allow you to control when an application pool is recycled. You can specify that Internet Information Services (IIS) 7 recycle the application pool after a time interval (in minutes) or at a specific time each day. You can also configure IIS to base the recycle on the amount of virtual memory or physical memory that the worker process in the application pool is using or configure IIS to recycle the application pool after the worker process has processed a specific number of requests.
But this has a side-effect of putting the application offline during the time the pool is restarting, so if you have any user connected at that time it will lose its session. This can be minimized by restarting the application at a time you have no users connected, like at dawn.
The following config snippet set the application pool to daily recycle at 3:00 A.M.:
<add name="Example">
<recycling logEventOnRecycle="Schedule">
<periodicRestart>
<schedule>
<clear />
<add value="03:00:00" />
</schedule>
</periodicRestart>
</recycling>
<processModel identityType="NetworkService" shutdownTimeLimit="00:00:30" startupTimeLimit="00:00:30" />
</add>
I'd have a date variable with the last time the counter was reset, and check the date is "today" on every access to the counter.
Unless you have critical performance problems, I guess that'd be the way to go.
Sample easy-lazy code to call whenever you are updating the counter:
lock(myCounter)
{
if(DateTime.Now.Date != lastDateCounterWasReset)
{
lastDateCounterWasReset = DateTime.Now.Date;
myCounter = 0;
}
myCounter++;
}
Now we'd need to know more about how you'd like to be storing those variables (myCounter
and lastDateCounterWasReset
), but could be basically anywhere (database, filesystem, etc.)
I suppose you could use the Application_BeginRequest
method. Use a boolean to see if it's already run that day.
Another option is a scheduler with a hidden URL to reset.
来源:https://stackoverflow.com/questions/15246998/how-to-reset-an-application-variable-daily