【问题标题】:WebService Call Exception: Thread was being abortedWebService 调用异常:线程被中止
【发布时间】:2012-02-23 12:13:16
【问题描述】:

最近我们从 ASP.NET 网络服务中获取 System.Threading.ThreadAbortExceptions,该网络服务每晚将数据发布到支付服务器。

网络服务是一个标准的 .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 的使用有关。

谢谢

/詹斯

【问题讨论】:

    标签: c# asp.net web-services


    【解决方案1】:

    我也有类似的问题。我认为 IIS 正在终止您的线程,因为它花费了太长时间。根据您的描述“我有一个遍历数据库中许多支付行的 foreach 循环”,您可以重组您的应用程序以单独处理每一行。创建一个 webmethod 将行发送到客户端,然后让客户端一次迭代一个事务并在不同的 webmethod 中处理它们。然后您可以处理发生的异常并继续进行。您甚至可以使用 Parallel.ForEach 一次处理多个

    【讨论】:

      【解决方案2】:

      前几天,我在 web 服务上遇到了类似的问题,并在 web.config 的 </system.web> 中添加了这一行:<httpRuntime executionTimeout="600"/>。 600 是直到被 asp.net 关闭的秒数。默认值为 110 秒。 它在这里解释了better 它的作用。

      【讨论】:

        【解决方案3】:

        经过一些 RND 尝试几件事:

        1. 尝试在应用程序池中添加自定义帐户
        2. 尝试通过运行 Microsoft 官方网站的 NetFxRepairTool exe 来修复 .net 框架
        3. 尝试添加 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-23
          • 2011-06-12
          • 2011-04-15
          • 1970-01-01
          • 2010-11-04
          • 1970-01-01
          相关资源
          最近更新 更多