Extension method(Wait() and Status()) does not work

Asked

Viewed 201 times

-1

This is not working(just the Wait() and Status lines())

try
            {
                string url = $"http://localhost:2710/api/faturamento/{IdUsuario}/{IdGaragem}";
                var uri = string.Format(url);
                var response = await client.GetStringAsync(uri);

                response.Wait(); // AQUI DÁ O ERRO

                while (response.Status != System.Threading.Tasks.TaskStatus.RanToCompletion)// AQUI DÁ O ERRO
                {

                }

                var desvio = JsonConvert.DeserializeObject<List<DesvioFaturamento>>(response);
                return desvio;
            }
            catch(Exception ex)
            {
                string erro = ex.Message;

                return null;
            }

makes that mistake:

"string" does not contain a definition for "Wait" and could not find no extension method "Wait" that accepts a first "string" type argument (there is a directive of use or reference of missing Assembly?)

"string" does not contain a definition for "Status" and could not find no extension method "Status" "string" type argument (there is a directive of use or reference of missing Assembly?)

P.S. I have another Xamarin.Forms project that these lines are working and have the same references.

EDIT1

This one is working

public async Task<List<LiberacaoItensGrid>> GetDataGrid(double id)
        {
            try
            {
                //var client = new HttpClient();
                //string url = $"http://www.inetglobal.com.br/autorizador/api/getliberaitens/{id}";

         //var response = await client.GetStringAsync(url);
                //var itenslib = JsonConvert.DeserializeObject<List<LiberacaoItensGrid>>(response);

                var client = new HttpClient();
                string url = $"http://www.inetglobal.com.br/autorizador/api/getliberaitens/{id}";
                var response = client.GetStringAsync(url);

                response.Wait(); // use assim ou com o while ....

                while (response.Status != System.Threading.Tasks.TaskStatus.RanToCompletion)
                {

                }
                var itenslib = JsonConvert.DeserializeObject<List<LiberacaoItensGrid>>(response.Result.ToString());

                return itenslib.ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
  • Where is the definition/declaration of the extension method?

  • @LINQ, what do you mean, where is the definition? I don’t understand. I’ll do an edit and show another project that has no mistake, the way you are I’ll post.

1 answer

1


The method Wait is a method of Task and respose is a string.

Note that in the first code has the keyword await, this causes the task to be solved and the variable response be the type string.

Your code is like this

var response = await client.GetStringAsync(uri);

Should be

var response = client.GetStringAsync(uri);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.