
在ASP.NET程序中我们经常遇到错误消息为"Request Timed Out"的HttpException,浏览器中的出错信息如下图所示,

同时应用程序日志中也会出现一条警告消息如下

如何重现这种异常?
线程在处理逻辑的过程中是如何被打断而抛出超时异常?
如何调试这种异常?
这篇文章我们从CLR源代码级别来分析一下该问题发生的原因以及相应的调试方法。
重现问题
首先我们先尝试重现这个问题,在ASP.NET程序web.config中有一个executionTimeout的设置,默认为110秒,也就是说一个请求的执行时间超过110秒就会timeout从而产生HttpException "Request Timed Out"。requesttimedout这个设置可以通过IIS Configuration Editor或者web.config进行更改。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings />
<connectionStrings />
<system.web>
<compilation>
</compilation>
<authentication mode="Windows" />
<httpRuntime executionTimeout="10" />
</system.web>
</configuration>
为了快速重现这个问题我们将executionTimeout默认改为10秒。然后建立一个让请求超时的asp.net页面如下。
<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(120000);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-36730-1.html
接下来是应该看我们的了