Serializing an object from HttpApplicationState

N

Natan

Hi!

I have an object stored in the HttpApplicationState collection of my
site, but i would like to mantain the state of this object even if the
application restart, so in case of any problem or when I change
something in the web.config or add anything to the bin directory, i can
load the last values from somewhere and continue the work.

I'm using serialization to accomplish this, and it works ok. But i'm
serializing the object to a file in the "HttpApplication.EndRequest"
event. The problem is that with this event, it saves the file in the end
of every instance of HttpApplication, which occours almost every second.
I tested locally, and it saves the file everytime i access any page.

I know that the HttpApplicationState is shared among all the instances
of HttpApplication, and I would like to save the file only before
ApplicationState is destroyed, not the application...

There is no event in HttpApplicationState, so is there any way of doing
this???


Thanks in advance.
 
S

Scott Allen

Hi Natan:

One of the safest things you could do is re-save the file anytime the
value changes (if it is not changing too frequently). This would take
care of even catostrophic failures.

There is an Application_End method you can use in the code behind for
Global.asax. This method should fire whenever the application shuts
down or recycles.
 
N

Natan

Scott said:
Hi Natan:

One of the safest things you could do is re-save the file anytime the
value changes (if it is not changing too frequently). This would take
care of even catostrophic failures.

That's what i would like to avoid. File writes are too expensive, and
this would be like 3 or 4 per second. although the file size is aprox.
20k, it is still unnecessary.
There is an Application_End method you can use in the code behind for
Global.asax. This method should fire whenever the application shuts
down or recycles.

Application_End is tied to the EndRequest actually, which is not the
Application in IIS itself, but HttpApplication instance. Many instances
are created and destroyed everytime by the server... take a look at the
documentation.

I really would like to save it only when necessary. Maybe i'll need to
implement it in another way...
 
S

Scott Allen

Hi Natan:
Application_End is tied to the EndRequest actually, which is not the
Application in IIS itself, but HttpApplication instance. Many instances
are created and destroyed everytime by the server... take a look at the
documentation.

Negative: Application_EndRequest fires as the last event in the
pipeline before the request moves to the HttpHandler for the requested
resource. This happens on every request. Furthermore, the
HttpApplication instance is not destroyed, but is kept in a pool to
service another request.

Application_End, like Application_Start, is invoked only once, and
only invoked on a single HttpApplication instance, the first instance
in the pool. It will fire before the application domain unloads,
although there is some evidence that this behavior is not 100.00%
reliable.

I've been through the documentation many times, but even better I have
code that can demonstrate this behavior. You can watch the output
window of the debugger as you make requests to the web app with the
following code in global. Do an IISRESET to watch Application_End
fire.

public Global()
{
InitializeComponent();
id = System.Threading.Interlocked.Increment(ref counter);
}

protected void Application_Start(Object sender, EventArgs e)
{
Debug.WriteLine("Application_Start on ID: " + id.ToString());
}

protected void Application_EndRequest(Object sender, EventArgs e)
{
Debug.WriteLine("Application_EndRequest on ID: " + id.ToString());
}

protected void Application_End(Object sender, EventArgs e)
{
Debug.WriteLine("Application_End on ID: " + id.ToString());
}


static int counter = 0;
int id = 0;

Hope this helps,
 
N

Natan

Scott said:
Negative: Application_EndRequest fires as the last event in the
pipeline before the request moves to the HttpHandler for the requested
resource. This happens on every request. Furthermore, the
HttpApplication instance is not destroyed, but is kept in a pool to
service another request.

Application_End, like Application_Start, is invoked only once, and
only invoked on a single HttpApplication instance, the first instance
in the pool. It will fire before the application domain unloads,
although there is some evidence that this behavior is not 100.00%
reliable.

Hmmm... thanks. The sdk documentation has some wrong links.

Anyway, the problem with Application_End is that the applicationstate
object is already destroyed, so I can't use it...

=/
 
N

Natan

Hi Scott!

Suddenly I had the brilliant idea of creating a destructor for my object
(duh... why didn't i thought about it before?), and applying a singleton
pattern for it, so there is only one instance of it and when it is
destroyed, no matter where he is, it serializes itself to the file. When
the application restarts it checks for the file and load the state again.

Thanks anyway!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top