C# HttpWebRequest GetRequestStream()-线程被中止错误消息
我有一个服务,它运行许多concurent操作,并使用HttpWebRequest发出outboudn请求 我偶尔会从GetRequestStream()调用中得到一个异常,该调用提供以下堆栈跟踪:C# HttpWebRequest GetRequestStream()-线程被中止错误消息,c#,.net,multithreading,httpwebrequest,C#,.net,Multithreading,Httpwebrequest,我有一个服务,它运行许多concurent操作,并使用HttpWebRequest发出outboudn请求 我偶尔会从GetRequestStream()调用中得到一个异常,该调用提供以下堆栈跟踪: Thread was being aborted. at System.Threading.WaitHandle.WaitOneNative(SafeHandle waitableSafeHandle, UInt32 millisecondsTimeout, Boolean hasThreadAff
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();
}
}
执行线程被IIS终止。将执行超时值更改为非常大的值,或通过以下方式之一禁用它:
部分中添加
dispose()
方法在内部为这些对象调用close()
。考虑使用< /Cult>块来实现<代码>。这是造成这个问题的原因吗?不,只是一个建议。这个代码是在后台线程中执行的吗?抛出了什么异常?“线程正在中止”是消息。需要异常类型。它是IIS应用程序吗?