2

During my application's Application_Start() event I want to fire off a HTTP request to an address. I absolutely do not want any cost incurred by Application_Start() and for App Start to be delayed in any fashion. I do not care about the response from the request.

What is the most correct method for firing this request in a completely non-blocking fashion?

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • `Task.Run(() => callToAddressCode);` Although there will be a *slight* cost, since the server is still doing something extra. – Pete Garafano Aug 20 '14 at 15:26
  • Look at this question - http://stackoverflow.com/questions/25295479/async-await-blocking-ui-wp8 make separate request method call it on startup - make a fake task in it that just return quickly followed by your main requested. – loop Aug 20 '14 at 15:31
  • You have to `await` the call to `client.GetAsync`, even though you aren't awaiting the `Task.Run`, any asynchronous operations within that method, should be awaited. Or, you can just add a `.Result` to the `GetAsync` since it being synchronous here shouldn't be a big deal. – Pete Garafano Aug 21 '14 at 14:26
  • The `Task.Delay().Wait()` is forcing the first thread to wait 5 seconds, in that time, it allows the `GetAsync` to complete. You can uncomment the `responseTask.Result` line which will force the method to wait until the `GetAsync` completes or you can just do `client.GetAsync().Wait()` and there is no need to capture the `responseTask` or `Task.Delay`. – Pete Garafano Aug 21 '14 at 15:19

3 Answers3

4

I would do something like this:

Task.Run(() =>
{
        using (var httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri("https://something.com/");
            var content = new StringContent("content");
            var result = httpClient.PostAsync("path/to/Service" , content);
            result.Wait();
        }
});
Michael Daw
  • 250
  • 3
  • 7
  • @ChrisMarisic I looked at this again, and yes you will need the call to result.Wait() in order for the call to PostAsync to complete. I've edited the code I posted. I was a little confused when I took this code from an old project of mine. I hope it is a little clearer now. – Michael Daw Aug 21 '14 at 14:37
  • For clarity, without the `Wait()` the using statement scope is immediately exited invokes dispose on `httpClient` which cancels the in-flight http post – Chris Marisic May 10 '16 at 19:50
2

I would recommend just firing the request asynchronously

using(var client = new HttpClient())
{
    client.PostAsJsonAsync("{url}",content)
}

for example will fire the request and continue

theDarse
  • 749
  • 5
  • 13
2

You can use WebClient.UploadStringAsync Method for this purpose

This method does not block the calling thread.

It can throw

  1. ArgumentNullException
  2. WebException

use it in this way

    var client = new WebClient();
    //remove comment from next line to execute some code after UploadString complete
    //client.UploadStringCompleted += new UploadStringCompletedEventHandler(function name to execute when finish);
    var data = "data to send";
    client.Headers.Add("Content-Type", "content type here");         
    client.UploadStringAsync(new Uri("http://www.yourDomain.com"), data);
faby
  • 7,394
  • 3
  • 27
  • 44