473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Serializing an object from HttpApplicationState

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.
Nov 18 '05 #1
5 2109
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.

--
Scott
http://www.OdeToCode.com

On Sun, 26 Sep 2004 19:06:03 -0300, Natan <nv***@mandic.com.br> wrote:
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.


Nov 18 '05 #2
Scott Allen wrote:
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...
Nov 18 '05 #3
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,

--
Scott
http://www.OdeToCode.com
Nov 18 '05 #4
Scott Allen wrote:
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...

=/
Nov 18 '05 #5
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!
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
6
by: Rein Petersen | last post by:
Hi Folks! Here's a strange behaviour: Without a properties SET accessor (see code below), the property will not serialize. public class myObject {
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
4
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm...
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
0
by: Ajay Choudhary | last post by:
Hi, I have a custom SoapExtension class and in the "ProcessMessage" method I would like to get a handle of HttpApplicationState object. How can I do that ? Thanks, Ajay
3
by: axr | last post by:
Having trouble with Serilization of objects that contain members which are of type Interface eg public class SomeClass { ISomeInterface1 itf1; ClassType1 ct1; ISomeInterface2 itf2;
4
by: mookid8000 | last post by:
Good day group! I have created a nice filtering plugin system, where all filters derive from a Filter class, and they pass a PictureData object between them. I have a problem though. I am able...
2
by: Charles Law | last post by:
I have a complex object that I am serializing, and I would like to omit elements that have a default value. For example, if I have a class as follows Public Class Test Private m_Name As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.