ユーザーレコードのExcelドキュメントをダウンロードする機能を持つ古いWebサイトを継承しました。HttpResponse.Writeの「リモートホストが接続を終了しました」エラー
: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);
}
}
}
ここで(「ダウンロード」ボタンをクリックすることで)それを呼び出すコードの例です:次のコードは、「。リモートホストが接続をクローズし、エラーコードが0x800704CDです」エラーを引き起こしています
using (OutputFileWriter writer = new OutputFileWriter(this.Response, "users.xls"))
{
foreach (string result in searchResults)
{
writer.WriteLine(result);
}
}
エラーが発生するのは、わずか数バイトのダウンロードでも発生します。クライアントがダウンロードをキャンセルした場合、通常の状況でエラーが発生する可能性があることを認識していますが、ユーザーがキャンセルしないとエラーが発生しています。接続が失われていると私は推測しますが、私は理由は分かりません。
関連性がある場合は、IIS 7.0で.NET 2.0の下に統合アプリケーションプールを使用してサイトを構成します。また、ロードバランスされています。
どのような考えですか?
[このIDisposableインターフェイスの適切な使用に関するこの回答]を提供する可能性があります(http://stackoverflow.com/a/538238/215552)a。私はあなたが間違っていると言っているわけではありませんが、あなたが 'IDisposable'を扱っている時には、いつも二重チェックするのは良い考えです。 –
@MikeMcCaughan奇妙なのは、Visual Studioでローカルで正常に動作しているようです。 IISまたはアプリケーションプールの設定を指している可能性があります。 – user806982