Q:

HttpWebRequest async call execution order when exception occurs

I used HttpWebRequest to async call a web service, occasionally EndGetResponse() returns some exceptions. However, even I got the exception, the call is successful and the data are returned. The following is my code with error checking code removed. I got 4 log items and the order is like this:
The following result is returned: ... 
   GetResult is successful!
   PostXML exception: ... 
   GetResult exception: ...
    public async Task<string> GetResult(string xmlData)
    {
        try
        {
            string response = await PostXML(Url, xmlData).ConfigureAwait(false);
            _logger.Log($"GetResult is successful!");
            return response;
        }
        catch (Exception ex)
        {
            _logger.Log("GetResult exception: " + ex.ToString());
            throw;
        }
    }

    private async Task<string> PostXML(string destinationUrl, string requestXml)
    {
        try
        {
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)await Task.Factory
                .FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                string responseStr = new StreamReader(responseStream).ReadToEnd();
                _logger.Log("The following result is returned: \r\n" + responseStr);
                responseStream.Close();
                return responseStr;
            }
        }
        catch (Exception ex)
        {
            _logger.Log("PostXML exception: " + ex.ToString());
            throw;
        }
        return null;
    }

The exception is like this:
PostXML() exception: System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MyService.d__7.MoveNext()
I guess I haven't fully understood how HttpWebRequest works in async mode. Anyway I hope these questions can help me to understand it more:
1. Why does the execution continue even an exception occurs? In my understanding, there should only have 2 log items to log the exception.
2. What is the reason the exception "The underlying connection was closed" occurs? Why does the exception occur but the service still gave back the correct result?

A:

Why does the execution continue even an exception occurs? In my understanding, there should only have 2 log items to log the exception.

This isn't possible. I suspect your code is calling GetResult twice. That's what the log seems to indicate.

What is the reason the exception "The underlying connection was closed" occurs?

This happens when the server closes the connection before the client is ready for it to be closed. This is an unusual error to get from a REST API, but not unheard of.

