Error return method "public async Taskstring"

3

When I execute the code below, it gives error in this line:

Literal1.Text= ObterEmocoes(imageFilePath);

You are giving this error message:

  

An object reference is required for the non-static field, method, or "Program.ObterEmocoes (string)" property

Code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        string imageFilePath = @"C:\Users\madureira\Downloads\JRMJ.jpg";
        Literal1.Text= ObterEmocoes(imageFilePath);
    }

    public async Task<string> ObterEmocoes(string imagemBase64)
    {
        HttpResponseMessage respostaHttp;
        string json;
        byte[] bytesImagem = Convert.FromBase64String(imagemBase64);

        string url = "https://brazilsouth.api.cognitive.microsoft.com/face/v1.0/detect";
        string queryString = "returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,emotion";

        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "2b4d806c1cf5467bb8772f86c3fc0a2e");
        using (var conteudoEmBytes = new ByteArrayContent(bytesImagem))
        {
            conteudoEmBytes.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            respostaHttp = await httpClient.PostAsync($"{url}?{queryString}", conteudoEmBytes);
            json = await respostaHttp.Content.ReadAsStringAsync();
        }
        return json;
    }
}
    
asked by anonymous 27.08.2018 / 03:16

1 answer

1

The error in this line is because you need to use the keyword await before calling the method.

Literal1.Text= await ObterEmocoes(imageFilePath);

For this you need to mark the Button1_Click method with the keyword async thus:

protected async void Button1_Click(object sender, EventArgs e)

Implementation:

protected async void Button1_Click(object sender, EventArgs e)
{
    string imageFilePath = @"C:\Users\madureira\Downloads\JRMJ.jpg";
    Literal1.Text= await ObterEmocoes(imageFilePath);
}

Reference

What is the difference between async Task and void?

In C #, what is the keyword await?

    
27.08.2018 / 03:46