3

Quando executo o código abaixo, dá erro nesta linha:

Literal1.Text= ObterEmocoes(imageFilePath);

Está dando esta mensagem de erro:

Uma referência de objeto é necessária para o campo, o método ou a propriedade "Program.ObterEmocoes(string)" não estática

Código:

    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;
    }
}
2
  • você... traduziu a mensagem de erro?
    – Daniel
    27/08/2018 às 1:21
  • 1
    A mensagem é clara, você precisa criar uma instância da classe. Do jeito que está chamando o método deveria ser estático 27/08/2018 às 1:24

1 Resposta 1

2

O erro nesta linha se dá por que você necessita utilizar a keyword await antes de chamar o método.

Literal1.Text= await ObterEmocoes(imageFilePath);

Para isso você precisa marcar o método Button1_Click com a keyword async ficando assim:

protected async void Button1_Click(object sender, EventArgs e)

Implementação:

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

Referencia

Qual diferença entre async Task e void?

Em C#, para que serve a palavra chave await?

1
  • 1
    Deu certo fazendo isso e acrescentando o "<%@ Page Async="true ..." no ASP. NET. Muito obrigado!!!!
    – madujr
    27/08/2018 às 17:42

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .