【发布时间】:2020-06-01 19:59:24
【问题描述】:
我正在尝试在 IIS 中停止此警告,并且我正在阅读我应该在调用 TransmitFile(filename) 之前检查此对象 IsClientConnected 。这是正确的还是另一种纠正方法?
IIS 异常
异常信息: 异常类型:HttpException 异常消息:远程主机关闭了连接。错误代码为 0x800703E3。 在 System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 结果,布尔值 ?throwOnDisconnect)
if (context.Response.IsClientConnected)
{
context.Response.Clear();
context.Response.ClearContent();
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.AddHeader("Expires", "-1");
context.Response.ContentType = "application/pdf";
context.Response.TransmitFile(filename);
context.Response.Flush();
context.Response.End();
}
else
{
context.Response.End();
}
更新代码
try
{
context.Response.Clear();
context.Response.ClearContent();
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.AddHeader("Expires", "-1");
context.Response.ContentType = "application/pdf";
context.Response.TransmitFile(filename);
context.ApplicationInstance.CompleteRequest()
//context.Response.Flush();
//context.Response.End();
}
catch (HttpException hex)
{
}
【问题讨论】:
-
不要打电话给
Flush和End。让服务器为您处理。 -
认为处理它的唯一方法是
catch异常。 -
@JohnWu 它只是一个 catch(HttpException hex) 还是有别的什么?
-
@DanielA.White 是
Flush和End有问题吗?TransmitFile呢?` -
我相信传输文件应该没问题