Scenario : Sending a POST
to 2 different endpoints (not related to or dependent on each other).
I'm really just not sure if I went about this correctly, and whether or not the responses will have returned from the context before the method returns. I have read some about async/await and the context for which it operates, etc., but I'm still not sure.
class Program
{
static void Main(string[] args)
{
Task.Run(async () =>
{
await SendRequests(cookie, request1, request2);
})
.GetAwaiter()
.GetResult();
}
static async Task<List<HttpResponseMessage>> SendRequests(Cookie cookie, HttpRequestMessage punchRequest, HttpRequestMessage request2)
{
List<HttpResponseMessage> responseMessages = new List<HttpResponseMessage>();
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
cookies.Add(cookie);
using (var httpClient = new HttpClient(handler))
{
var response1 = await httpClient.SendAsync(request1);
var response2 = await httpClient.SendAsync(request2);
responseMessages.AddRange(new [] { response1 , response2 });
return responseMessages;
}
}
}
I'm wondering if :
Task.Run( async () => ....)
is correct here, as well asGetAwaiter/GetResult
- Can the
SendRequests
method ever return before the requests finish / get resolved?