I test concurrency like this: Open 2 tabs in Chrome and visit localhost:5006/test almost simultaneously.
I want to achieve that I send 2 requests almost simultaneously and then getting 2 responses almost simultaneously 10 seconds later, but in reality I send 2 requests almost simultaneously and get the first response 10 seconds later, and then get the second response another 10 seconds
I have tried using asynchronous endpoint but it doesn't work.
Here is my code.
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
TestController.cs
using Microsoft.AspNetCore.Mvc;
namespace Test.Controllers;
[ApiController]
[Route("/test")]
public class TestController : ControllerBase
{
public async Task<IActionResult> Echo()
{
Console.WriteLine("new requests come");
await Task.Delay(10000); //Time-consuming operations
return Ok("Hello");
}
}
launchSettings.json
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:62699",
"sslPort": 44389
}
},
"profiles": {
"Test": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5006",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}