Simultaneous calls in RESTful service

4

Hello, I am having doubts on how to make fun calls (about 100) simultaneous on a REST service.

The code example I have is the following:

using (var http = new HttpClient { BaseAddress = new Uri("some url") })
{
     using (var httpContent = new StringContent(json, Encoding.Default, "application/json"))
            {
                using (var response = await http.PostAsync("/services/send", httpContent))
                {
                }
            }
}

I would like to know if I need to use await and async or if the httpClient class already has something by default, and if it is possible to make only one connection and send several requests within the same connection

    
asked by anonymous 13.09.2017 / 05:52

2 answers

1
  

[...] To do this I need to use await and async or if the httpClient class already has something by default [...]

The class HttpClient does not it has native support for the management of multiple requests.

One possibility is the parallel invocation via Parallel.ForEach :

await Task.Run(() => Parallel.ForEach(listaDeUrls, MetodoASerChamado));

Where MetodoASerChamado receives a single parameter, a member of the listaDeUrls collection.

  

You can only make one connection and send multiple requests within the same connection.

According to RFC 7230 it is possible to perform multiple sequential requests in> using a single connection. The current HTTP protocol specification (1.1) does not support parallel requests on a single connection.

    
13.09.2017 / 15:14
0

Hello,

To make multiple calls you needed to use await and async. Well, if the requests were not waiting for the next one.

As informed by Guilherme you can learn about it here: link

    
13.09.2017 / 14:26