webRequest.GetResponse()抛出ThreadAbortException

时间:2016-05-19 07:21:25

标签: c# .net threadabortexception

我正在运行同步作业,它将数据同步到api,我每天都会收到几次ThreadAbortException,我无法弄清楚原因。代码看起来像这样。

var result = new TResult();

        try
        {
            using (var response = webRequest.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream == null)
                    {
                        return result;
                    }

                    using (var reader = new StreamReader(responseStream))
                    {
                        string content = reader.ReadToEnd();

                        result.HandleResponse(response, content);
                    }
                }

            }
        }
        catch (WebException ex)
        {
            using (var responseStream = ex.Response.GetResponseStream())
            {
                if (responseStream == null)
                {
                    result.HandleError(ex);
                }

                using (var reader = new StreamReader(responseStream))
                {
                    string content = reader.ReadToEnd();

                    result.HandleError(ex, content);
                }
            }
        }

        return result;    

以下是抛出的异常:

System.Threading.ThreadAbortException: Thread was being aborted.
 at System.Net.ConnectStream.WriteHeaders(Boolean async)
 at System.Net.HttpWebRequest.EndSubmitRequest()
 at System.Net.HttpWebRequest.CheckDeferredCallDone(ConnectStream stream)
 at System.Net.HttpWebRequest.GetResponse()

我已经google了,大多数答案似乎围绕着这个错误:https://support.microsoft.com/en-us/kb/312629

错误的原因可能是什么?我应该如何避免或处理错误?

0 个答案:

没有答案