Why is this web api controller not concurrent?

后端 未结 5 836
旧巷少年郎
旧巷少年郎 2020-12-01 08:32

I have a Web API Controller with the following method inside:

public string Tester()
{
    Thread.Sleep(2000);

    return \"OK\";
}

When I

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 09:05

    Look at this question, which is relative to your problem: Are all the web requests executed in parallel and handled asynchronously?

    It states that:

    A webApi service, by default, executes all its incoming requests in parallel, but only if the current multiple requests (at a certain time) came from different sessions. That is to say, if single client will send some simultaneously requests to server, all of them will be executed sequentially and won't be executed concurrently.

    To solve this, you can use async methods, as described here.

    Generally, you need to declare your method as async, like this:

    public async Task GetObj()
    

提交回复
热议问题