Share via


how long can a singleton exist in a web application in IIS?

Question

Wednesday, August 20, 2014 2:06 PM

how long can a singleton exist in a web application in IIS?

if it created an instance in memory and managed by IIS,

will it be GC.collect after no one use web application for a long time?

if not restart IIS, does it mean this singleton can exist forever?

if restart IIS, will singleton be deleted and lose all its state?

if restart IIS will lose data of singleton, any method to solve singleton problem or replace singleton pattern?

找到我了, 你又能怎樣呢? 我又能怎樣呢?

All replies (3)

Wednesday, August 20, 2014 5:48 PM ✅Answered

how long can a singleton exist in a web application in IIS?

It's gone the moment the client is done with it,  unless you take means to keep an object in session and alive across sessions. Web based solution are stateless unless you take special means to keep things like objects in state.

if it created an instance in memory and managed by IIS,

will it be GC.collect after no one use web application for a long time?

That is never going to happen. It's gone the moment the client application returns to the client side, because Web based applications a stateless applications.  And the Web server knows nothing about a previous client session, unless the developer develops  means  in code to help the Web server remember the client's previous session.   

if not restart IIS, does it mean this singleton can exist forever?

It can exist if you make it so by using code to keep the object in session and alive across sessions.

if restart IIS, will singleton be deleted and lose all its state?

Everything is lost the moment IIS is restarted. Everything is lost if the ASP.NET Worker Process that is hosting an Application Pool the ASP.NET application is running in is recycled.

if restart IIS will lose data of singleton, any method to solve singleton problem or replace singleton pattern?

Start the application up again and get the singleton object back in session so that it can be used across sessions.


Wednesday, August 20, 2014 2:22 PM

>>how long can a singleton exist in a web application in IIS?
From the first request to it, i.e. when the singleton is created, until the application pool in which the ASP.NET application is runnning is recycled for any reason.

>>if it created an instance in memory and managed by IIS, will it be GC.collect after no one use web application for a long time?
No, static fields are not garbage collection while the class is loaded.

>>if not restart IIS, does it mean this singleton can exist forever?
Theoretically, yes. From the firs request to it until the applicaion pool is recycled.

>>if restart IIS, will singleton be deleted and lose all its state?
Yes, an ASP.NET application lives in an application pool and when IIS is restarted the application is recycled and the singleton disappears.

>>if restart IIS will lose data of singleton, any method to solve singleton problem or replace singleton pattern?
You will have to store the state of the application outside IIS, for example in a database on a SQL Server.

Please note that you should ask ASP.NET and web related question in the following forum and not here: http://forums.asp.net

Also, please remember to mark any helpful posts as answer and/or helpful.

Edit: Just to clarify after having read some other replies: A new application does not get created for each new request. The application lives longer than a single request and the singleton will be cross request and cross session. Please refer to the following page for more information: http://adeneys.wordpress.com/2011/06/01/singleton-patterns-for-asp-net/


Wednesday, August 20, 2014 6:54 PM

Darnold is correct.

.

There is very little point in using Singleton in a web application.

Very little.

As Darnold says, web applications are stateless.

You could cache a Singleton, so it was there between round trips but that's kind of pointless since you could instantiate any class as an object and cache it.

The request comes in.... is processed and the response sent back.

Done.

There is no process running there in memory per user to hold a static class.

Hope that helps
Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.