Serializing an object from HttpApplicationState

Discussion in 'ASP .Net' started by Natan, Sep 26, 2004.

  1. Natan

    Natan Guest

    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.
     
    Natan, Sep 26, 2004
    #1
    1. Advertising

  2. Natan

    Scott Allen Guest

    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 <> 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.
     
    Scott Allen, Sep 27, 2004
    #2
    1. Advertising

  3. Natan

    Natan Guest

    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...
     
    Natan, Sep 27, 2004
    #3
  4. Natan

    Scott Allen Guest

    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
     
    Scott Allen, Sep 27, 2004
    #4
  5. Natan

    Natan Guest

    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...

    =/
     
    Natan, Sep 27, 2004
    #5
  6. Natan

    Natan Guest

    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!
     
    Natan, Sep 27, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajesh.V

    HttpApplicationState question..

    Rajesh.V, Jan 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    530
    Paul Glavich
    Jan 7, 2004
  2. moi
    Replies:
    2
    Views:
    680
  3. mnowosad

    HttpApplicationState data being lost

    mnowosad, Sep 16, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    788
    Guest
    Sep 17, 2008
  4. Ajay Choudhary

    Accessing HttpApplicationState object from SoapExtension

    Ajay Choudhary, May 16, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    173
    Brock Allen
    May 18, 2005
  5. Brian

    Cache or HttpApplicationState

    Brian, Oct 18, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    222
    Brian
    Oct 18, 2005
Loading...

Share This Page