Is it possible to make a synchronous call to Webapi?

Asked

Viewed 925 times

0

  • It is possible to make non-synchrone calls to WebApi?

  • I don’t want my application to pass beaten by the method that executes the query to WebApi, I want her to expect a response and take an attitude as she returns, but all the examples I’ve seen of calls to WebApi sane Async. What I must do?

  • My architecture is what’s 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;
                    }
                }
            }
  • Impossible to answer this question without knowing which language will consume the API.

  • You use the API the way it is most convenient. What kind of example have you seen? front-end WEB applications?

  • I’m using C# in . net Webapi and a Xamarin app with c# as well

  • could add the code snippet where you call the api?

  • You want to know if there is a way to control the behavior of the client application that will consume your Webapi without you programming the client side. That’s it?

  • Guys, thank you so much for the help, responding to what Leandro asked and what Pagotti asked , I edited the question by putting the sample code excerpt. I tried to call the method that queries so List<Company> Listcompany = this.Getall<Company>(). Result But then the webapi query hangs and I have no idea why.

Show 1 more comment

2 answers

1


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;
}

0

It is possible to make the call synchronously, using the async keyword before the method call as shown below:

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

    dgvDados.DataSource = ListEmpresa; 
}

Mias very carefully, one 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.

  • Hi Julio, much for the answer, in case I take my problem was to make my application even mischief the screen, intentionally and I could not do that. Well, I finally got it. Here’s what I did

Browser other questions tagged

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