✅ Why do my ASP.NET Core Requests not run in parallel?

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/slow", async () => await Task.Delay(5000));
app.Run();
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/slow", async () => await Task.Delay(5000));
app.Run();
If I call the /slow enpoints multiple times in short succession, I can see that they are not processed in parallel. I can see that because they take longer than 5 seconds (except first one). This is confusing to me, as I would expect them to await the delay in parallel. Can someone explain why this is?
12 Replies
Tvde1
Tvde18mo ago
app.MapGet("/test",
async () =>
{
using var httpClient = new HttpClient();
var requests = new List<Task>();

var stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 10; i++)
{
var response = httpClient.GetAsync("http://localhost:5800/slow");
requests.Add(response);
}

await Task.WhenAll(requests);
stopwatch.Stop();
return Results.Ok($"Took {stopwatch.ElapsedMilliseconds}ms");
});
app.MapGet("/test",
async () =>
{
using var httpClient = new HttpClient();
var requests = new List<Task>();

var stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 10; i++)
{
var response = httpClient.GetAsync("http://localhost:5800/slow");
requests.Add(response);
}

await Task.WhenAll(requests);
stopwatch.Stop();
return Results.Ok($"Took {stopwatch.ElapsedMilliseconds}ms");
});
"Took 5125ms" they run in parallel on my pc at least
chef bildig duregspilt
yes, this also runs parallel on my pc, but have you tried my example?
Tvde1
Tvde18mo ago
yes, I added that to your example it calls your example
Tvde1
Tvde18mo ago
No description
chef bildig duregspilt
ahh, i see
chef bildig duregspilt
my logs tell me the request took 5 seconds, but they clearly take longer
No description
Tvde1
Tvde18mo ago
how are you measuring how long they take?
chef bildig duregspilt
i start two requests at the same time and see if they finish at the same time, but one of the two take roughly double the time hmm, if i test with your code it also takes 5000, it only happens if i send the requests with my browser
Tvde1
Tvde18mo ago
Does this answer the question?
Tvde1
Tvde18mo ago
Stack Overflow
Web api handles just 6 concurrent requests
I have a web api project using async/await controllers and tasks. I've noticed every request after the 6th one gets queued. To test this, I made an easy delayed action: [HttpGet()] [Route("
Tvde1
Tvde18mo ago
the browser limits the amount of concurrent requests :p
chef bildig duregspilt
yes, but i just open the url in 2 seperate tabs ok, tested it with postman, they run in parallel so it is a browser thing
Want results from more Discord servers?
Add your server
More Posts
❔ Help with Duende Login for part of pageI am having an issue that I have been struggeling with for quite some time now. I want only part of ❔ Validation Error auto resize parentHow to auto resize parent when has validation error? So that the error text doesn't crash into other❔ ✅ Coding Test Website in ASP.NETSo im making a website that should be able to login users (from school server) and Admin/Reader shou❔ Learning platformI'm currently learning C# at CS education. It's going great, but as we're dipping our toes into abst❔ guidance on protecting sensitive details (licence details) neededQ: does anyone have any guidance on protecting sensitive details (licence name and serial) from bein❔ Convert .docx to .pdf without restrictionsI need to make a .pdf from a .docx, but there is a problem I have been looking at libraries and they❔ JWT + Microsoft.AspNetCore.Authentication.JwtBearerI added to my small (Asp.net Webapi) project JSON Web Token from tutorial but I don't uderstand thi❔ Question about Azure AD B2C and Front-End Authentication via APIHey everyone! I've got a question about Azure AD B2C. After someone signs up using the "RegisterPort❔ ✅ Making sense of dijkstra's algorithm for grid based player movement and pathfinding.I need some assistance with a grid based movement system for the game I'm working on. If anyone's in✅ text document boring thing but i'm stuck >_<Hey guys, I'm trying to code a program in C# that takes a .txt as input and turns only the second co