P
philip.halfpenny
Hi All,
I have an application (windows service) that sends out multiple
HTTPWebRequests asynchronously. I am getting a problem intermittently
where I receive the error "The underlying connection was closed: The
request was canceled." on GetResponseStream(). Although the problem is
intermittent, the problems seem to happen in "bunches", i.e.
everything will be working properly, then a number of responses will
fail (e.g. 50 or more), then everything will carry on fine.
I am using Visual Studio .Net 2003 and .Net Framework 1.1. The service
is running on Windows Server 2003 if that makes any difference.
The relevant code is:
'########################### START CODE ###########################
Class RequestState
Public HTTPDataObj As HTTPDataObject '-- HTTPDataObject is just a
class that contains basic information
'--
for the request
Public RequestObj As HttpWebRequest
Sub New(ByVal HTTPData As HTTPDataObject, ByVal Request As
HttpWebRequest)
RequestObj = Request
HTTPDataObj = HTTPData
End Sub
End Class
Public Sub SendRequest(ByVal HTTPData As HTTPDataObject)
Dim Request As HttpWebRequest = HttpWebRequest.Create(URI)
Request.KeepAlive = True
Request.UserAgent = "HTTPAgent/1.0"
Request.Method = "GET"
Request.Timeout = 60000
'-- Asynchronously send the request
Dim State As New RequestState(HTTPData, Request)
Dim Result As IAsyncResult = CType(Request.BeginGetResponse(New
AsyncCallback(AddressOf ReceiveResult), State), IAsyncResult)
ThreadPool.RegisterWaitForSingleObject(Result.AsyncWaitHandle, New
WaitOrTimerCallback(AddressOf ScanTimeoutCallback), State, 60500,
True)
....
End Sub
Private Sub ReceiveResult(ByVal Result As IAsyncResult)
'-- Asynchronously receive the response
Dim State As RequestState = CType(Result.AsyncState, RequestState)
Dim SR As StreamReader
Dim Response As HttpWebResponse
If Not State.HTTPDataObj.TimedOut Then
Try
Dim Request As HttpWebRequest = State.RequestObj
State.HTTPDataObj.ResponseTimestamp =
Logging.GetDateTimeStamp(False)
Response = CType(Request.EndGetResponse(Result),
HttpWebResponse)
State.HTTPDataObj.ResponseCode = Response.StatusCode
SR = New StreamReader(Response.GetResponseStream()) '###
ERROR OCCURS HERE
State.HTTPDataObj.ResponseBody = SR.ReadToEnd()
Catch WebEx As WebException
If Not WebEx.Response Is Nothing Then
State.HTTPDataObj.ResponseCode = CType(WebEx.Response,
HttpWebResponse).StatusCode
SR = New StreamReader(CType(WebEx.Response,
HttpWebResponse).GetResponseStream)
State.HTTPDataObj.ResponseBody = SR.ReadToEnd()
CType(WebEx.Response, HttpWebResponse).Close()
Else
State.HTTPDataObj.ResponseCode = "999"
State.HTTPDataObj.ResponseBody = WebEx.Message
End If
Finally
If Not Response Is Nothing Then
Response.Close()
End If
If Not SR Is Nothing Then
SR.Close()
End If
End Try
....
End Sub
Private Sub ScanTimeoutCallback(ByVal State As Object, ByVal timedOut
As Boolean)
If timedOut Then
Dim reqState As RequestState = CType(State, RequestState)
Dim HTTPData As HTTPDataObject
If Not reqState Is Nothing Then
HTTPData = reqState.HTTPDataObj
HTTPData.TimedOut = True
reqState.RequestObj.Abort()
End If
End If
End Sub
'############################ END CODE ############################
There is also logging in there that I don't think is relevant to the
problem, so has been stripped out of the above code.
Looking through the logs that are written by the app, I seem to start
getting the error message just after a timeout. It then continues to
fail for a number of responses (doesn't seem to be the same amount
each time it happens), then it continues happily for a while. Based on
the information we have of the url that the requests are being sent
to, they seem to be receiving the majority of requests so the problem
seems to be occuring between them receiving the request and us
receiving the response.
Looking through the 'Net trying to find any information on this, the
only things that I've found is saying that the response needs to be
closed, which as far as I can tell, I'm doing correctly. The only
place that I'm not closing the response is on the timeout callback
because there is no response by that point and the request is aborted.
I'm open to any suggestions or links to any articles / sites that
might be helpful as I'm starting to run out of ideas now and can't
seem to find anything that's completely relevant on the 'Net.
Thanks in advance for any help or advice,
PhilHalf
I have an application (windows service) that sends out multiple
HTTPWebRequests asynchronously. I am getting a problem intermittently
where I receive the error "The underlying connection was closed: The
request was canceled." on GetResponseStream(). Although the problem is
intermittent, the problems seem to happen in "bunches", i.e.
everything will be working properly, then a number of responses will
fail (e.g. 50 or more), then everything will carry on fine.
I am using Visual Studio .Net 2003 and .Net Framework 1.1. The service
is running on Windows Server 2003 if that makes any difference.
The relevant code is:
'########################### START CODE ###########################
Class RequestState
Public HTTPDataObj As HTTPDataObject '-- HTTPDataObject is just a
class that contains basic information
'--
for the request
Public RequestObj As HttpWebRequest
Sub New(ByVal HTTPData As HTTPDataObject, ByVal Request As
HttpWebRequest)
RequestObj = Request
HTTPDataObj = HTTPData
End Sub
End Class
Public Sub SendRequest(ByVal HTTPData As HTTPDataObject)
Dim Request As HttpWebRequest = HttpWebRequest.Create(URI)
Request.KeepAlive = True
Request.UserAgent = "HTTPAgent/1.0"
Request.Method = "GET"
Request.Timeout = 60000
'-- Asynchronously send the request
Dim State As New RequestState(HTTPData, Request)
Dim Result As IAsyncResult = CType(Request.BeginGetResponse(New
AsyncCallback(AddressOf ReceiveResult), State), IAsyncResult)
ThreadPool.RegisterWaitForSingleObject(Result.AsyncWaitHandle, New
WaitOrTimerCallback(AddressOf ScanTimeoutCallback), State, 60500,
True)
....
End Sub
Private Sub ReceiveResult(ByVal Result As IAsyncResult)
'-- Asynchronously receive the response
Dim State As RequestState = CType(Result.AsyncState, RequestState)
Dim SR As StreamReader
Dim Response As HttpWebResponse
If Not State.HTTPDataObj.TimedOut Then
Try
Dim Request As HttpWebRequest = State.RequestObj
State.HTTPDataObj.ResponseTimestamp =
Logging.GetDateTimeStamp(False)
Response = CType(Request.EndGetResponse(Result),
HttpWebResponse)
State.HTTPDataObj.ResponseCode = Response.StatusCode
SR = New StreamReader(Response.GetResponseStream()) '###
ERROR OCCURS HERE
State.HTTPDataObj.ResponseBody = SR.ReadToEnd()
Catch WebEx As WebException
If Not WebEx.Response Is Nothing Then
State.HTTPDataObj.ResponseCode = CType(WebEx.Response,
HttpWebResponse).StatusCode
SR = New StreamReader(CType(WebEx.Response,
HttpWebResponse).GetResponseStream)
State.HTTPDataObj.ResponseBody = SR.ReadToEnd()
CType(WebEx.Response, HttpWebResponse).Close()
Else
State.HTTPDataObj.ResponseCode = "999"
State.HTTPDataObj.ResponseBody = WebEx.Message
End If
Finally
If Not Response Is Nothing Then
Response.Close()
End If
If Not SR Is Nothing Then
SR.Close()
End If
End Try
....
End Sub
Private Sub ScanTimeoutCallback(ByVal State As Object, ByVal timedOut
As Boolean)
If timedOut Then
Dim reqState As RequestState = CType(State, RequestState)
Dim HTTPData As HTTPDataObject
If Not reqState Is Nothing Then
HTTPData = reqState.HTTPDataObj
HTTPData.TimedOut = True
reqState.RequestObj.Abort()
End If
End If
End Sub
'############################ END CODE ############################
There is also logging in there that I don't think is relevant to the
problem, so has been stripped out of the above code.
Looking through the logs that are written by the app, I seem to start
getting the error message just after a timeout. It then continues to
fail for a number of responses (doesn't seem to be the same amount
each time it happens), then it continues happily for a while. Based on
the information we have of the url that the requests are being sent
to, they seem to be receiving the majority of requests so the problem
seems to be occuring between them receiving the request and us
receiving the response.
Looking through the 'Net trying to find any information on this, the
only things that I've found is saying that the response needs to be
closed, which as far as I can tell, I'm doing correctly. The only
place that I'm not closing the response is on the timeout callback
because there is no response by that point and the request is aborted.
I'm open to any suggestions or links to any articles / sites that
might be helpful as I'm starting to run out of ideas now and can't
seem to find anything that's completely relevant on the 'Net.
Thanks in advance for any help or advice,
PhilHalf