locked
the response was already flushed by the application RRS feed

  • Question

  • User379720387 posted

    This stackoverflow thread describes the problem I am having with a listener page that is being hit with a POST from an Azure function once a day.

    When this code runs (once a day) all it does is does some checks against certain columns in the db and then does stuff.

    At the end of the page I have this:

    Response.Clear();
        Response.ContentType = "text/html; charset=UTF-8";
        Response.Write(responseMsg);
        Response.Flush();
        Response.End();

    This results in this error message:

    System.Web.HttpExceptionSession state has created a session id, but cannot save it because the response was already flushed by the application.
    
    System.Web.HttpException (0x80004005): Session state has created a session id, but cannot save it because the response was already flushed by the application.
       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.ReleaseStateGetSessionID()
       at System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs)
       at System.Web.SessionState.SessionStateModule.OnEndRequest(Object source, EventArgs eventArgs)
       at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    		

    A solution is suggested to modify the global.asax file.

    Well, I am using Web Pages 3.2, as far as I know there is no such file.  Where would I put this in my environment?

    Sunday, April 19, 2020 11:54 AM

Answers

All replies

  • User475983607 posted

    You can get errors like this when using the async/await pattern incorrectly.  You can also get this type if error the POST invokes a ASP.NET Web Form application that is designed to access Session on all requests.  The web form page is returning a response before the page life cycle gets to update Session.  

    Perhaps disable Session on this Web Page directive; https://support.microsoft.com/en-us/help/306996/how-to-disable-asp-session-state-in-asp-net

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" 
    AutoEventWireup="false" Inherits="WebApplication1.WebForm1" 
    EnableSessionState="false" %>

    Sunday, April 19, 2020 12:26 PM
  • User379720387 posted

    You are adding to the confusion!

    I am not using async/await that I know. And my application is Web Pages not Web Forms.

    Monday, April 20, 2020 1:00 AM
  • User475983607 posted

    You are adding to the confusion!

    I am not using async/await that I know. And my application is Web Pages not Web Forms.

    Thanks for clarifying your code snippet.  Web Pages have a page directive too. The documentation cover the page directive attributes.

    https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ydy4x04a(v=vs.100)?redirectedfrom=MSDN

    Monday, April 20, 2020 10:22 AM
  • User1535942433 posted

    Hi wavemaster,

    As far as I think,you could remove Response.Flush()  and set Response.Buffer default value to be true.

    The Response.Flush method is used to output the buffer data to the browser immediately. When you  call the Response.Flush method multiple times, the browser will accept data multiple times instead of only once.At the same time,when you set the value of Response.Buffer to true, the data will be cached in the buffer, and after the page is processed, the contents of the buffer will be sent to the client all at once.

    More details,you could refer to below articles:

    Response.Flush:

    https://docs.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525560(v=vs.90)

    Response. BufferOutput:

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponse.bufferoutput?view=netframework-4.8

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 21, 2020 7:58 AM
  • User379720387 posted

    This is what I ended up with:

            Response.ContentType = "text/html; charset=UTF-8";
            Response.Clear();
            Response.Write(responseMsg);
            Response.BufferOutput = true;
    
            Response.End();

    This seems to be working, but I am wondering of the .Clear() is in the right place.

    Anyone?

    Sunday, April 26, 2020 8:59 PM
  • User1535942433 posted

    Hi wavemaster,

    Accroding to your description,as far as I think, clear() method will remove other content that might be attached to the response.If you want to send responseMsg,write() is called after clear().

    Best regards,

    Yijing Sun

    Monday, April 27, 2020 5:36 AM
  • User379720387 posted

    I made the changes as per my previous post and I am no longer see the error messages in my logs.

    Appears to be working.

    Thanks.

    Friday, May 1, 2020 6:23 PM