Simultaneous calls in Restful service

Asked

Viewed 401 times

4

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

The example of code 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 wonder if to do this I need to use await and async or if the class httpClient already has something by default, and if it is possible to make only one connection and send several requests within the same connection.

  • This should answer: https://msdn.microsoft.com/en-us/library/hh696703(v=vs.110). aspx :)

2 answers

2

[...] 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 have native support for managing multiple requests.

A possibility is parallel invocation via Parallel.Foreach:

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

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

it is possible to make only one connection and send several requests within the same connection.

According to the RFC 7230 it is possible to make several requests sequential using a single connection. The current HTTP protocol specification (1.1) does not support parallel requests on a single connection.

0

Browser other questions tagged

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