I have a static helper method class and at the base of it, I have this async
method:
private static async Task<string> SendRequest(HttpMethod method, string apiUrl, string json, ILogger logger)
{
try
{
using var request = new HttpRequestMessage()
{
Method = method,
RequestUri = new Uri(apiUrl, UriKind.Absolute),
};
if (!string.IsNullOrWhiteSpace(json))
{
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
request.Content.Headers.ContentType.MediaType = MediaTypeNames.Application.Json;
}
using var client = new HttpClient();
HttpResponseMessage result = await client.SendAsync(request);
if (result.IsSuccessStatusCode)
{
return await result.Content.ReadAsStringAsync();
}
else
{
return $"{apiUrl}, {result.StatusCode}: Error from {method} request";
}
}
catch (Exception ex)
{
logger.LogCritical(ex, $"{method} request failed: {apiUrl}");
return $"{apiUrl}, {ex.Message}";
}
}
Does it matter if the methods that call this are async or not it seems I can do either of the following:
internal static Task<string> SendPutRequest(string apiUrl, string json, ILogger logger)
{
return SendRequest(HttpMethod.Put, apiUrl, json, logger);
}
or
internal static async Task<string> SendPutRequest(string apiUrl, string json, ILogger logger)
{
return await SendRequest(HttpMethod.Put, apiUrl, json, logger);
}
It has no difference to my end call which is in an async
method and calls await SendPutRequest
so I was wondering if it there was any point if the SendPutRequest was marked as async
or not.