Async webrequest times out => IIS failed

I have a web application that receives data from external services. The request itself occurs, as in the code below, as far as I understand. Create a request, run it asynchronously, and allow the return message to process the response. Works well in my community environment.

public static void MakeRequest(Uri uri, Action<Stream> responseCallback) { WebRequest request = WebRequest.Create(uri); request.Proxy = null; request.Timeout = 8000; try { Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null) .ContinueWith(task => { WebResponse response = task.Result; Stream responseStream = response.GetResponseStream(); responseCallback(response.GetResponseStream()); responseStream.Close(); response.Close(); }); } catch (Exception ex) { _log.Error("MakeRequest to " + uri + " went wrong.", ex); } } 

However, external test and production environments, for reasons beyond my control, cannot reach the destination URL. Well, I thought, the request timeout will not hurt anyone. However, it seemed that every time this request was interrupted, ASP.NET crashed and IIS was restarted. The event log shows me, among other things, this stack:

 StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task`1.get_Result() at MyApp.AsyncWebClient.<>c__DisplayClass2.<MakeRequest>b__0(Task`1 task) at System.Threading.Tasks.Task`1.<>c__DisplayClass17.<ContinueWith>b__16(Object obj) at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute() InnerException: System.Net.WebException Message: Unable to connect to the remote server StackTrace: at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, TaskCompletionSource`1 tcs) InnerException: System.Net.Sockets.SocketException Message: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond StackTrace: at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) 

.. so it all comes down to a SocketException, it seems to me. And right after that (within the same second) another event is recorded (which, I assume, is relevant):

 Exception Info: System.AggregateException Stack: at System.Threading.Tasks.TaskExceptionHolder.Finalize() 

This is something like me, because I don’t own asynchronous code and streaming, but the timeout from the web requests causes IIS to crash, it seems very strange. I MakeRequest to execute queries synchronously, which works great. The request still does not work in these environments, but no damage occurs and the application continues to work happily forever after.

So, I decided to solve my problem, but why is this happening? Can someone enlighten me ?:-)

+6
source share
1 answer

Your continuation should take into account the fact that .Result may reflect an exception. Otherwise, you have an unhandled exception. Unhandled exceptions kill processes.

 .ContinueWith(task => { try { WebResponse response = task.Result; Stream responseStream = response.GetResponseStream(); responseCallback(responseStream); responseStream.Close(); response.Close(); } catch(Exception ex) { // TODO: log ex, etc } }); 

your old exception handler only covers task creation, not a callback.

+12
source

Source: https://habr.com/ru/post/917017/


All Articles