Question

I have developed an Asp.Net MVC Web Application and deployed it on IIS 8 Server and in my application I am using a list to store online users and display them in a page using the following code

if (HttpRuntime.Cache["LoggedInUsers"] != null)
                {
                    List<string> loggedInUsers = (List<string>)HttpRuntime.Cache["LoggedInUsers"];

                    if (loggedInUsers.Contains(model.UserName))
                    {
                    }
                    else
                    {
                        loggedInUsers.Add(model.UserName);
                        HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers;
                    }
                }

For some reason, the list gets cleared every night and when I look for the active users, I see it empty.

Is it something that has to be dealt on IIS 8 or is there any better way to implement the Online users using a database table may be..

Was it helpful?

Solution

IIS can recycle your application pool (for several reasons, including idling and no requests, too much memory use, etc...) At that point your application will be unloaded, and then loaded again later. Hence your cache values are gone.

Second, do you have any code that at some point prunes and removes old entries from cache? If not, it means you got a memory leak as it'll continue to grow indefinitely (and thereby trigger application pool recycle).

If you do have prunning code (so the cache is actively managed to avoid indefinite growth), and you need its contents to survive past pool restarts, then you have few options:

  • Use database. Simply have a table of active users and add/delete there.
    • Pro: survives even unexpected crashes of app, iis, and even machine itself.
    • Con: Slow due to db access and db contention point possibilities.
  • Put code in your application start / end event handlers to serialize contents to a file on end, and deserialize on start.
    • Pro: faster than db. works during graceful shutdowns.
    • Con: will not work due to unexpected crash.

OTHER TIPS

Your site probably shuts down after a certain amount of time when there is no activity. Look at IIS settings for Application Pools (more specifically "Set idle-timeout to never in IIS") on google...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top