Use async without waiting when I don't need an answer

I want to send SMS from my application. An SMS will send when I send a request for a specific URL. All my methods are asynchronous, but when I am an example HttpClientand want to use response.Content.ReadAsStringAsync(), I deleted await.

I do not want to wait for the response of this method and I want to send a request only to this URL. Now can you tell me this is a good solution?

This is my sample code:

public async Task<bool> SendMessage(string number, string body)
{
    var from = _config["SMSSenderSettings:FromNumber"];
        var username = _config["SMSSenderSettings:PanelUserName"];
        var password = _config["SMSSenderSettings:PanelPassword"];

        using (var client = new HttpClient())
        {
            try
            {
                var response = await client.GetAsync($"{BaseUrl}/send.php?method=sendsms&format=json&from={from}" +
                    $"&to={number}&text={body}&type=0&username={username}&password={password}");
                response.EnsureSuccessStatusCode(); // Throw exception if call is not successful

                response.Content.ReadAsStringAsync();
                return true;
            }
            catch (HttpRequestException)
            {
                return false;
            }
        }
}

I deleted awaitfrom response.Content.ReadAsStringAsync();and I received a warning.

+4
source share
3 answers

If you do not want to wait for your task, you can remove the unnecessary return type

public async Task SendMessage(string number, string body)
{
    var from = _config["SMSSenderSettings:FromNumber"];
    var username = _config["SMSSenderSettings:PanelUserName"];
    var password = _config["SMSSenderSettings:PanelPassword"];

    using (var client = new HttpClient())
    {
        try
        {
            var response = await client.GetAsync($"{BaseUrl}/send.php?method=sendsms&format=json&from={from}" +
                $"&to={number}&text={body}&type=0&username={username}&password={password}");
            response.EnsureSuccessStatusCode(); // Throw exception if call is not successful

            await response.Content.ReadAsStringAsync();
        }
        catch (HttpRequestException)
        {

        }
    }
}

SendMessage , -

await SendMessage().ConfigureAwait(false);

. , , .

. -

async /?

async #

+3

, - await/Wait/.Result Task, , . docs

, . , , .

var t = response.Content.ReadAsStringAsync();

, .

+2

If the content of the response is not required, do not read the content at all. And you should not throw exceptions when you can avoid it.

You can have much cleaner code with this.

public async Task<bool> SendMessage(string number, string body)
{
    var from = _config["SMSSenderSettings:FromNumber"];
    var username = _config["SMSSenderSettings:PanelUserName"];
    var password = _config["SMSSenderSettings:PanelPassword"];

    using (var client = new HttpClient())
    {
        var response = await client.GetAsync($"{BaseUrl}/send.php?method=sendsms&format=json&from={from}" +
            $"&to={number}&text={body}&type=0&username={username}&password={password}");
        return response.IsSuccessStatusCode();
    }
}
0
source

Source: https://habr.com/ru/post/1684972/


All Articles