Why does ASP.NET terminates async requests?

Viewed 54

I know that this is not the proper way to write code, but I still like to understand this behavior - whenever I do an async HTTP request without waiting for the result, it seems like the request is terminated? (I do not see it on fiddler).

Code sample:

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost.fiddlerhttpclient/");
                    var response = client.GetAsync("zzzz");
                }
            }
        }
    }

If I change the code to:

var response = client.GetAsync("zzzz").Result;

I do see it on fiddler. I don't understand why - I would expect that the request would still keep processing in background somehow, so why doesn't it?

1 Answers

Disposing HttpClient cancels all pending requests, if any. You are disposing your client (because of using block) immediately after starting request with GetAsync, so there is almost no chance for it to start making actual http request which will appear in fiddler.

Related