0

I have been working on a simple c# application that uses rest and Azure SQL Database. My app has two buttons one for saving new names and the second for showing them. the later doesn't react when i press it and i get the following error related to these parts GetRequest(); PostRequest(jsonPost);

        private void Btn_Click(object sender, EventArgs e)
        {
            GetRequest();
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            User user = new User(0, firstnameText.Text);
            JavaScriptSerializer java = new JavaScriptSerializer();
            String jsonPost = java.Serialize(user);

            PostRequest(jsonPost);
        }
    }

Severity Code Description Project File Line Suppression State Detail Description Warning CS4014 Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. PersonRegister C:\Users\User\Downloads\RestSolution\RestSolution\PersonRegister\Form1.cs 86 Active The current method calls an async method that returns a Task or a Task and doesn't apply the await operator to the result. The call to the async method starts an asynchronous task. However, because no await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't what you expect. Usually other aspects of the calling method depend on the results of the call or, minimally, the called method is expected to complete before you return from the method that contains the call.

An equally important issue is what happens to exceptions that are raised in the called async method. An exception that's raised in a method that returns a Task or Task is stored in the returned task. If you don't await the task or explicitly check for exceptions, the exception is lost. If you await the task, its exception is rethrown.

As a best practice, you should always await the call.

You should consider suppressing the warning only if you're sure that you don't want to wait for the asynchronous call to complete and that the called method won't raise any exceptions. In that case, you can suppress the warning by assigning the task result of the call to a variable.

Pro
  • 81
  • 1
  • 9

1 Answers1

2

Although the advice to always return Task from an async method, this is not possible for UI event handlers. As such you should rewrite your handler like this:

private async void Button1_Click(object sender, EventArgs e)
{
    await GetRequest();
}

private async void Button2_Click(object sender, EventArgs e)
{
        Person p = new Person(0, nameText.Text);
        JavaScriptSerializer js = new JavaScriptSerializer();
        String jsonPost = js.Serialize(p);

        await PostRequest(jsonPost);
}

See here for more info.

Also, as an aside, you should declare one static instance of HttpClient for the entire application (as per MSDN docs).

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    [Here](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) is a more detailed explanation of the HttpClient issue. – Kirk Larkin Oct 22 '17 at 15:24