Share via


Error 0x80072746 The remote host closed the connection.

Question

Tuesday, May 16, 2006 5:48 AM

Hi everyone, I'm getting the following error;

Error message:
System.Web.HttpException: The remote host closed the connection. The error code is 0x80072746.
   at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async)
   at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)
   at System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush)
   at System.Web.HttpResponse.Flush(Boolean finalFlush)
   at System.Web.HttpResponse.Flush()
   at downloadwindow.loadResourceInfo() in .CS PAGE:line 55
   at ASP.ASPX PAGE__Render__control1(HtmlTextWriter __w, Control parameterContainer) in .ASPX PAGE:line 12
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Page.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
***************************************************************************

It only give's this error when the page is accessed while uploaded onto a certain server, it does not happen on our local server.

The code is;

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net;
using System.IO;

public partial class downloadwindow : System.Web.UI.Page
{
    // asp element declarations

    public void loadResourceInfo()
    {
        ////////////////////////////////
        int downloads = 0;
        string fileName;
        ////////////////////////////////

        using (SqlConnection con = new SqlConnection()) // create new database connection
        {
            con.ConnectionString = (ConfigurationSettings.AppSettings["ConnectionString"]);

            SqlParameter nrid = new SqlParameter("@nrid", SqlDbType.NVarChar);
            nrid.Value = ID;

            con.Open();

            using (SqlCommand a = new SqlCommand("SELECT FilePath FROM tblDLR WHERE DLRID=@rid", con))
            {
                a.Parameters.Add(rid);
                fileName = a.ExecuteScalar().ToString();
            }

            using (SqlCommand b = new SqlCommand("SELECT DH FROM tblDLR WHERE DLRID=@nrid", con))
            {
                b.Parameters.Add(nrid);
                downloads = Convert.ToInt16(b.ExecuteScalar());
                downloads++;
            }
            FileInfo fileInfo = new FileInfo(Server.MapPath("../" + fileName));

            this.Response.Clear();
            this.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            this.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            this.Response.ContentType = "application/octet-stream";
            this.Response.WriteFile(fileInfo.FullName);
            this.Response.Flush();
            this.Response.End();
        }
    }
}

Does anybody have any ideas or has at least had this error before, all help is most welcome.

Thanks!

 

All replies (4)

Tuesday, June 6, 2006 9:52 AM

We just got the same error today with the same stack trace.  It ocurred after loading a new build of our web application onto a test server.  In our case we are opening a page in a new window, but instead of rendering the page we are writing xml output (Excel spreadsheet format). 

this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
this.Response.ClearContent();
this.Response.ClearHeaders();
this.Response.ContentType = "application/vnd.ms-excel";

// HEADER
this.Response.Write(ContractsDetailResource.ContractDetailHeader);

// write the row count
this.Response.Write((oDetailViewTable.Rows.Count + 2).ToString());

// HEADER ROW
this.Response.Write(ContractsDetailResource.ContractDetailHeaderRow);

// ITEM ROWS
DetailViewParameterProvider oDetailViewParameterProvider = new DetailViewParameterProvider();
foreach (DataRow oRow in oDetailViewTable.Rows)
{
    string sRow = "foo";// Details omitted
    this.Response.Write(sRow);
}

// FOOTER ROW
this.Response.Write(ContractsDetailResource.ContractDetailFooterRow);
// FOOTER
this.Response.Write(ContractsDetailResource.ContractDetailFooter);

this.Response.Flush();
this.Response.Close();
this.Response.End();
We have struggled a lot with getting Excel downloads to work.  The symptoms in this case are the file downloads fine to target _blank.  The second time it downloads fine to a new window but the previous window goes blank.  The third download hangs until the other two download windows are closed.
The error does not manifest itself in the development environment (VS2005 running on IIS).  We believe the problem with the windows to be caused by MS Excel, but the exception is new to us.
Our work-around for this problem is to write the output to temporary files (they automatically clean themselves up during garbage collection) and to redirect to the temporary file:
 string sTempDir = "temp/";
TempFileCollection oTempFiles = new TempFileCollection(this.MapPathSecure(sTempDir), false);
string sFilePathName = oTempFiles.AddExtension("xml", false);
string sFileName = Path.GetFileName(sFilePathName);
string sFileUrl = String.Format("{0}{1}", sTempDir, sFileName);

this.Session[sFileName] = oTempFiles; // keep the file around while the session is active

using (StreamWriter sw = File.CreateText(sFilePathName))
{
    // HEADER
    sw.Write(ContractsDetailResource.ContractDetailHeader);

    // write the row count
    sw.Write((oDetailViewTable.Rows.Count + 2).ToString());

    // HEADER ROW
    sw.Write(ContractsDetailResource.ContractDetailHeaderRow);

    // ITEM ROWS
    foreach (DataRow oRow in oDetailViewTable.Rows)
    {
        string sRow = "foo"; // Details omitted
        sw.Write(sRow);
    }

    // FOOTER ROW
    sw.Write(ContractsDetailResource.ContractDetailFooterRow);
    // FOOTER
    sw.Write(ContractsDetailResource.ContractDetailFooter);
}

this.Response.Redirect(sFileUrl);

This works fine if you are willing to establish a temporary folder that can be written to by your ASPNET or NETWORK SERVICE account.

 


Tuesday, June 6, 2006 2:30 PM

Thanks for the reply. I discovered the error only occured in the following scenario;

A user begins a download, during that download they decide to cancel. Therefore Response.Flush() does not complete; why? Because; "The remote host closed the connection".

It was a real pain, but luckily, the client decided that this information could be useful and wanted to receive the error emails as it would indicate which downloads people were not prepared to wait for.

We later found a solution that may or may not have worked (we don't know as we didn't want to upload a new version when they were happy with the current solution.

The possible solution that we found was to set the buffer in the page directive (where the Language = C# etc stuff goes).

Annoyingly I can't even remember whether you have to set it to true or false, I'm thinking probably false. So try putting Buffer=false in the directive and see if that helps, then try true if it doesnt.

Apparently removing the Flush() will solve the problem but may undoubtedly cause new ones, but worth a mention I guess.

Hope this helps.

Gren


Thursday, August 26, 2010 1:16 AM

hi **guyswartwout **,

Its a great post.

I have one issue , the temp files had n't delete after session was expired.

Wht can i do for it, plz help me


Tuesday, April 16, 2013 11:41 AM

You have tu set Response.Buffer = true;

Other case flush method will throw an exception