"The remote host closed the connection" error for HttpResponse.Write

0

I've inherited an old website that has a feature for downloading an excel document of user records. The following code is causing a "The remote host closed the connection. The error code is 0x800704CD. " error:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace MySite.ApplicationServices
{
public class OutputFileWriter : IDisposable
{
    private HttpResponse _response;
    private bool _isResponsePrepared;

    public OutputFileWriter(HttpResponse response)
    {
        this._response = response;
    }

    public OutputFileWriter(HttpResponse response, string outputFileName)
        : this(response)
    {
        this.OutputFileName = outputFileName;
    }

    public string OutputFileName { get; set; }

    public virtual void WriteLine(string line)
    {
        if (this._response == null)
            throw new ObjectDisposedException("OutputFileWriter");

        if (!this._isResponsePrepared)
        {
            this.PrepareResponse();
            this._isResponsePrepared = true;
        }

        this._response.Write(line);
    }

    public virtual void Dispose()
    {
        if (this._response != null)
        {
            this._response.Flush();
            this._response.Close();
            this._response = null;
        }
    }

    protected virtual void PrepareResponse()
    {
        if (string.IsNullOrEmpty(this.OutputFileName))
            throw new InvalidOperationException("An output file name is required.");

        this._response.Clear();
        this._response.ContentType = "application/octet-stream";
        this._response.Buffer = this._response.BufferOutput = false;
        this._response.AppendHeader("Cache-Control", "no-store, no-cache");
        this._response.AppendHeader("Expires", "-1");
        this._response.AppendHeader("Content-disposition", "attachment; filename=" + this.OutputFileName);
    }
}
} 

Here's an example of the code that calls it (on clicking a 'Download' button):

 using (OutputFileWriter writer = new OutputFileWriter(this.Response, "users.xls"))
            {
                foreach (string result in searchResults)
                {
                    writer.WriteLine(result);
                }
            }

The error occurs even after just a few bytes downloaded. I'm aware that the error can occur under normal circumstances if a client cancels the download, but it is happening when people don't cancel too. The connection is being lost I presume, but I'm not sure why.

In case it's relevant, the site is configured in IIS 7 with Integrated App pool under .NET 2.0. It is also load balanced.

Any thoughts?

.net
httpresponse
asked on Stack Overflow Nov 15, 2016 by user806982

1 Answer

0

After some further investigation, it seems the file could in fact be downloaded in Firefox only.

The following post suggested changing from _response.Close() to _response.End() might work https://productforums.google.com/forum/#!topic/chrome/8Aykgxa8kWU, which it did.

However, I then saw this HttpResponse.End vs HttpResponse.Close vs HttpResponse.SuppressContent

So I'm now calling CompleteRequest() on the HttpApplication instance. Like this:

Old method

public virtual void Dispose()
{
    if (this._response != null)
    {
        this._response.Flush();
        this._response.Close();
        this._response = null;
    }
}

New method:

public virtual void Dispose()
{
    if (this._response != null)
    {
        this._response.Flush();
        this._application.CompleteRequest();
        this._response = null;
    }
}

This is now working fine.

answered on Stack Overflow Nov 18, 2016 by user806982 • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0