I have the following two methods that I use to make fire and forget accesses to http addresses. I originally used ThreadPool.QueueUserWorkItem, but was recommended to use Async Await because of problems with thread exhaustion, as this method could be called often.
First question: which one is better?
Second question - is the correct implementation of async running? Since I am debugging it, it seems synchronous, which is a big problem, since I need to free the calling asp.net thread and return the response to the user without waiting for the HTTP call to complete.
Call -
Send("http://example.com");
Method -
public static void Send(string url)
{
ThreadPool.QueueUserWorkItem(
o =>
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.Timeout = 5000;
var response = request.GetResponse();
response.Close();
}
catch (Exception ex)
{
SendException(ex, url);
}
});
}
ASYNC / AWAIT -
Call -
await SendAsync("http://example.com");
Method -
public async static Task SendAsync(string url)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.Timeout = 5000;
var response = await request.GetResponseAsync().ConfigureAwait(false);
response.Close();
}
catch (Exception ex)
{
SendException(ex, url);
}
}