Is it possible to make a synchronous call to WebApi?

0
  • Can non-asynchronous calls be made to WebApi ?

  • I do not want my application to get hit by the method that runs the query with WebApi , I want it to wait for a response and take action according to the return, but all examples I saw of calls to WebApi are Async . What should I do?

  • Is my architecture wrong?

In this example I would like to load the grid

    private void btnListaGenerica_Click(object sender, EventArgs e)
    {
        Task<List<Empresa>> ListEmpresa = this.GetAll<Empresa>(); 

        dgvDados.DataSource = ListEmpresa; 
    }

    private async Task<List<T>> GetAll<T>()
    {
        try
        { 
            using (var client = new HttpClient())
            {
                using (var response = await client.GetAsync("http://localhost:49783/api/empresas/listall"))
               if (response.IsSuccessStatusCode)
                    {
                        //clienteUri = response.Headers.Location;
                        var ProdutoJsonString = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<T[]>(ProdutoJsonString).ToList();
                    }
                    else
                    {
                         return null;
                    }
                }
            }
    
asked by anonymous 31.01.2017 / 22:31

2 answers

0
private void btnListaGenerica_Click(object sender, EventArgs e)
{

    List<Empresa> list = Task.Run(() => GetAll<Empresa>("empresas/listall")).Result;

    dgvDados.DataSource = list;

}

public List<T> GetAll<T>(string endereco)
{
    List<T> list = new List<T>();

    try
    {
        using (var client = new HttpClient())
        {
            using (var response = client.GetAsync("http://localhost:49783/api/" + endereco).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    var ProdutoJsonString = response.Content.ReadAsStringAsync().Result;
                    list = JsonConvert.DeserializeObject<T[]>(ProdutoJsonString).ToList();
                }
                else
                {
                    MessageBox.Show("Não foi possível obter o produto : " + response.StatusCode);
                    return null;
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Eitcha!");
        return null;
    }
    return list;
}
    
01.02.2017 / 18:12
0

You can make the call synchronously using the async keyword before calling the method as exemplified below:

private void btnListaGenerica_Click(object sender, EventArgs e)
{
    List<Empresa> ListEmpresa = await this.GetAll<Empresa>(); 

    dgvDados.DataSource = ListEmpresa; 
}

As you are very careful, you should also evaluate whether the synchronous call will not block your Main Thread causing a screen locking impression.

For further clarification on the use of async and await, see this link .

    
01.02.2017 / 18:03