HttpWebRequest GetRequestStream() - 线程被中止错误消息

时间:2013-07-23 15:34:07

标签: c# .net multithreading httpwebrequest

我有一个运行大量conccurent操作的服务,并使用HttpWebRequest发出outboudn请求。

我偶尔会从GetRequestStream()调用中获得一个异常,该异常提供以下堆栈跟踪:

Thread was being aborted.
at System.Threading.WaitHandle.WaitOneNative(SafeHandle waitableSafeHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
at System.Threading.WaitHandle.InternalWaitOne(SafeHandle waitableSafeHandle, Int64  millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
at System.Net.LazyAsyncResult.WaitForCompletion(Boolean snap)
at System.Net.Connection.SubmitRequest(HttpWebRequest request, Boolean forcedsubmit)
at System.Net.ServicePoint.SubmitRequest(HttpWebRequest request, String connName)
at System.Net.HttpWebRequest.SubmitRequest(ServicePoint servicePoint)
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
//Rest of stack trace is path to my internal call

我用来处理导致此问题的出站请求的代码如下:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.PathToOutboundAPI.com/ApiCall);
request.Method = "POST";
request.Headers.Add("SOAPAction", Action);
request.ContentType = "text/xml";
request.KeepAlive = false;
request.ServicePoint.Expect100Continue = false;
request.ServicePoint.ConnectionLeaseTimeout = 0;    //Dont want Connections staying open
var soapRequest = CreateSoapEnvelope();

byte[] bytes = Encoding.ASCII.GetBytes(soapRequest);

string str = string.Empty;
Stream rs = default(Stream);
WebResponse response = default(WebResponse);
StreamReader reader = default(StreamReader);
try
{
    rs = request.GetRequestStream();
    rs.Write(bytes, 0, bytes.Length);
    response = request.GetResponse();
    reader = new StreamReader(response.GetResponseStream());
    str = reader.ReadToEnd();
}
catch (WebException exception)
{
//Handle Appropriately
}
catch(Exception exception)
{
    //Handle Appropriately
}
finally
{
    if (rs != null)
    {
        rs.Close();
        rs.Dispose();
    }

    if (reader != null)
    {
        reader.Close();
        reader.Dispose();
    }

    if (response != null)
    {
        response.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

执行线程被IIS终止。将执行超时值更改为非常大的值,或者通过以下方式之一禁用它:

  1. 将IIS应用程序池中应用程序的空闲超时值更改为较大的值,或将0(零)更改为禁用它。

  2. 在应用的<httpRuntime executionTimeout="1800"/>部分添加<system.web>