ChaseMedallion
ChaseMedallion

Reputation: 21764

What are the underlying causes of a WebException with message "The request was aborted: The request was canceled."

I am making Http requests using an HttpClient. I occasionally see the following error:

Exception Type: System.Net.WebException
Error message: The request was aborted: The request was canceled.
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
Exception Type: System.Net.Http.HttpRequestException
Error message: An error occurred while sending the request.

I am trying to understand what the root cause could be. There are a number of posts regarding this message that I could find on StackOverflow (e. g. this post), but all seemed related to finding workarounds (often things like setting KeepAlive to false which I do not want), where as I just want to understand what the error really means so I can decide whether and where to take action.

Upvotes: 4

Views: 2393

Answers (1)

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

Nobody can tell for sure.

Enabling network client trace can help detecting the root cause.

Note:

The document defines 5 trace sources, but there are 6 in the System.Net's Logging class:

s_WebTraceSource = new NclTraceSource("System.Net");
s_HttpListenerTraceSource = new NclTraceSource("System.Net.HttpListener"); // not in the documentation
s_SocketsTraceSource = new NclTraceSource("System.Net.Sockets");
s_WebSocketsTraceSource = new NclTraceSource("System.Net.WebSockets");
s_CacheTraceSource = new NclTraceSource("System.Net.Cache");
s_TraceSourceHttpName = new NclTraceSource("System.Net.Http");

Sample log when all 5 enabled:

System.Net Verbose: 0 : [9520] WebRequest::Create(http://localhost:9876/)
System.Net Verbose: 0 : [9520] HttpWebRequest#58870012::HttpWebRequest(http://localhost:9876/#363619410)
System.Net Information: 0 : [9520] Current OS installation type is 'Client'.
System.Net Information: 0 : [9520] RAS supported: True
System.Net Verbose: 0 : [9520] Exiting HttpWebRequest#58870012::HttpWebRequest() 
System.Net Verbose: 0 : [9520] Exiting WebRequest::Create()     -> HttpWebRequest#58870012
System.Net Verbose: 0 : [9520] HttpWebRequest#58870012::GetResponse()
System.Net Verbose: 0 : [9520] ServicePoint#33675143::ServicePoint(localhost:9876)
System.Net Information: 0 : [9520] Associating HttpWebRequest#58870012 with ServicePoint#33675143
System.Net Information: 0 : [9520] Associating Connection#34640832 with HttpWebRequest#58870012
System.Net.Sockets Verbose: 0 : [9520] Socket#43332040::Socket(AddressFamily#2)
System.Net.Sockets Verbose: 0 : [9520] Exiting Socket#43332040::Socket() 
System.Net.Sockets Verbose: 0 : [9520] Socket#54444047::Socket(AddressFamily#23)
System.Net.Sockets Verbose: 0 : [9520] Exiting Socket#54444047::Socket() 
System.Net.Sockets Verbose: 0 : [9520] DNS::TryInternalResolve(localhost)
System.Net.Sockets Verbose: 0 : [9520] Socket#43332040::Connect(127.0.0.1:9876#16787179)
System.Net.Sockets Error: 0 : [9520] Socket#43332040::UpdateStatusAfterSocketError() - ConnectionRefused
System.Net.Sockets Error: 0 : [9520] Exception in Socket#43332040::Connect - No connection could be made because the target machine actively refused it 127.0.0.1:9876.
System.Net.Sockets Verbose: 0 : [9520] Socket#43332040::Dispose()
System.Net.Sockets Verbose: 0 : [9520] Socket#54444047::Dispose()

Upvotes: 2

Related Questions