WebService调用异常:线程正在中止
本文关键字:线程 调用 异常 WebService | 更新日期: 2023-09-27 18:25:37
最近,我们从一个每晚向支付服务器发布数据的ASP.NET Web服务中获得了System.Threading.ThreadAbortExceptions。
该Web服务是从另一个客户端调用的标准.asmx页面。
在页面内部,我有一个foreach循环,它遍历数据库中的许多支付行:
foreach (var obtRow in readyToBeCaptured)
{
try
{
status = dibs.GetPagePost(...);
// handle status here
}
catch (ThreadAbortException tex)
{
SingletonLogger.Instance.Error(string.Format("Transactionhandler - Could not Capture, Thread was aborted. {0}", tex.Message), tex);
}
catch (Exception ex)
{
SingletonLogger.Instance.Error(string.Format("Transactionhandler - Could not Capture, statuscode: {0}, message: {1}.", status, ex.Message), ex);
}
}
奇怪的是,当这个错误发生时,我从catch(ThreadAbortException-tex)块中获得了一个日志条目,但随后代码中断,并在调用树的另一个try-catch块中被捕获。理想情况下,我会在foreach循环中有代码来继续
这是GetPagePost方法
private bool GetPagePost(string url, NameValueCollection nameValueCollection, string userName, string password)
{
WebClient client = new WebClient();
if (userName != "")
{
client.Credentials = new NetworkCredential(userName, password);
}
foreach (string str in nameValueCollection.AllKeys)
{
this._callCollection.Add(str, nameValueCollection[str]);
}
StringBuilder builder = new StringBuilder();
builder.Append("DIBS.NET/1.2.0 ( ");
builder.Append("OS ").Append(Environment.OSVersion.Platform).Append(" ").Append(Environment.OSVersion.Version).Append("; ");
builder.Append(".NET CLR ").Append(Environment.Version).Append("; ");
builder.Append("User ").Append(Environment.UserName).Append("; ");
builder.Append("Domain ").Append(Environment.UserDomainName).Append("; ");
builder.Append(" ) ");
client.Headers.Add("User-Agent", builder.ToString());
try
{
byte[] bytes = client.UploadValues(url, "POST", nameValueCollection);
this._httpStatus = 200;
this._httpBody = Encoding.UTF8.GetString(bytes);
return true;
}
catch (WebException exception)
{
this._httpBody = exception.Message;
this._httpStatus = (int) exception.Status;
}
catch (UriFormatException exception2)
{
this._httpBody = exception2.Message;
this._httpStatus = -9999;
}
catch (Exception exception3)
{
this._httpBody = exception3.Message;
this._httpStatus = -99999;
}
return false;
}}
为什么会出现这种错误,我如何防止代码从foreach循环中中断?我在StackOverflow上看到了其他一些帖子,但它们似乎与我不使用的Reponse.Redirect的用法有关。
感谢
/Jens
我也有类似的问题。我认为IIS正在终止您的线程,原因是占用的时间太长。根据您的描述"我有一个foreach循环,它遍历数据库中的许多支付行",您可能能够重组您的应用程序,以单独处理每一行。创建一个Web方法将行发送到客户端,然后让客户端一次迭代一个事务,并在不同的Web方法中处理它们。然后,您可以处理发生的异常并继续执行。您甚至可以使用Parallel.ForEach 同时处理多个
最近几天,我在一个web服务中遇到了类似的问题,并在web.config的</system.web>
中添加了以下行来解决这个问题:<httpRuntime executionTimeout="600"/>
。600是asp.net关闭之前的秒数。默认值为110秒。它在这里更好地解释了它的作用。
在一些RND之后尝试一些事情:
- 尝试在应用程序池中添加自定义帐户
- 尝试通过从Microsoft官方网站运行NetFxRepairTool exe来修复.net框架
- 尝试添加httpRuntime和targetFramework="4.5"(这对我有效targetFramework="4.5")
httpRuntime targetFramework="4.5"maxRequestLength="4000"executionTimeout="45"
参考:http://dotnetvisio.blogspot.com/2013/07/solution-for-thread-being-aborted-call.html