Session state has created a session id, but cannot save it because the response was already flushed by the application.

February 3rd, 2009 by jf | Filed under .NET, ASP.NET, C#, Cool Stuff, Internet Articles, My Life, Tech, Web, Work.

Lately i started getting an Exception of the type [HttpException] with message: Session state has created a session id, but cannot save it because the response was already flushed by the application.

?View Code CSHARP
Type : System.Web.HttpException, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Message : Session state has created a session id, but cannot save it because the response was already flushed by the application.
Source : System.Web
Help link :
ErrorCode : -2147467259
Data : System.Collections.ListDictionaryInternal
TargetSite : Void SaveSessionID(System.Web.HttpContext, System.String, Boolean ByRef, Boolean ByRef)
Stack Trace :    at System.Web.SessionState.SessionIDManager.SaveSessionID(HttpContext context, String id, Boolean& 
redirected, Boolean& cookieAdded)
at System.Web.SessionState.SessionStateModule.CreateSessionId()
at System.Web.SessionState.SessionStateModule.DelayedGetSessionId()
at System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

I have googled a bit on the net to find a solution and found out that this could be due to:

session state id is stored in a cookie. the cookie is a header, and must be
written before any content. if you do a flush, no headers can be written. in
1.1 if buffering was on (the default), flushes were not honored.

Ref: http://www.velocityreviews.com/forums/t301530-session-state-has-created-a-session-id-but-cannot-save-it-because-the-response-was-already-flushed-by-the-application.html

If you get an Exception saying “Session state has created a session ID, but cannot save it because the response was already flushed by the application” then make sure you have set DisplayWhenNewSession to ‘false’

you lose session state when the application pool is recycled (unless you r using sql server to store your sessions, of course) if that is your problem, and it occurs frequently, then someone missconfigured your webserver as app. pool recycling can be regulated in IIS. i personally think that the error message received sounds like its the app_pool… just a guess, but i’d take a look at the iis settings… try putting the app in a seperate pool for testing, or regulate the recyle settings.

Ref: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23779174.html

And finally there is a little tweak/fix that i found out:

That happens because sometimes (depending on the web.config configuration) the SessionID is not set in the cookie when Session_Srtart 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):

?View Code CSHARP
  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 </span>
	string sessionId = Session.SessionID;
    }

Ref: http://www.codeverge.net/ng.asp-net-forum.migrating_from_asp-net_1-x_to_asp-net_2-0/error-session-state-has-created-a-session-id-but-cannot-save-it-because-the-response-was-already-flushed-by-the-application

Tags: , ,

One Response to “Session state has created a session id, but cannot save it because the response was already flushed by the application.”

  1. Mike Nelson | 24/05/09

    I’m that message too sometimes. Thanks for compiling that. Did you try those solutions and find out which one worked? (ie setting DisplayWhenNewSession or doing the Session_Start fix)

Share Your Thoughts