【问题标题】:ASP.net host closed the connectionASP.net 主机关闭连接
【发布时间】: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)
            {

            }

【问题讨论】:

  • 不要打电话给FlushEnd。让服务器为您处理。
  • 认为处理它的唯一方法是catch异常。
  • @JohnWu 它只是一个 catch(HttpException hex) 还是有别的什么?
  • @DanielA.White 是FlushEnd 有问题吗? TransmitFile呢?`
  • 我相信传输文件应该没问题

标签: c# asp.net


【解决方案1】:

此错误意味着您的代码中存在运行时异常。通常,当通过 IIS 托管您的应用程序时,会出现这样的错误,这些错误会立即关闭连接。

您应该通过附加到进程w3wp来调试您的代码。

通过在 Visual Studio 中导航到:

---> DEBUG
   ---> Attach to process
   ---> choose w3wp.exe
   ---> attach to process

疑难解答程序暗示您检查 IsClientConnected 对象,因为它是可能导致异常的最常见问题 - 如果您尝试调用不存在的客户端(IsClientConnectednull示例)。

【讨论】:

  • 谢谢 我应该抓住错误并删除 IsClientConnected 吗?
  • 是的,那将是最好的选择。
猜你喜欢
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多