sk7730
sk7730

Reputation: 736

Session State Can not be Saved, It was already Flushed by Application

In my Web Application, i am getting an error. "Session state has created session ID. But It can not save it because it was already flushed by application".

I googled for this issue and found that i have to store session id in Global.asax Session_Start Event.

string id = Session.SessionID;

But it was already exist in my application. I am not sure what else is causing issue. I was not using Response.Flush() also.

Can anyone please explain about this issue & fix for it.

Upvotes: 2

Views: 10388

Answers (1)

Hardik Vinzava
Hardik Vinzava

Reputation: 988

That happens because sometimes (depending on the web.config configuration) the SessionID is not set in the cookie when Session_Start event executes in the global asax.

You encounter this error because at somepoint in the pagelifecycle a variable is set in the session. After the request ends, ASP.NET tries to set the SessionID too, but if the Request was flused (eg. this can be done by Response.Write or AJAX itself flushes the response) this exception will be thrown.

A simple fix would be (in the global.asax file):

void Session_Start(object sender, EventArgs e)
    {
// Code that runs when a new session is started


    //Ensure SessionID in order to prevent the folloing exception
    //when the Application Pool Recycles
    //[HttpException]: Session state has created a session id, but cannot
    //    save it because the response was already flushed by 
    string sessionId = Session.SessionID;
    }

Upvotes: 1

Related Questions