gpt4 book ai didi

c# - 将异步放在不执行等待操作的 WebAPI Controller 操作上有什么好处?

转载 作者:行者123 更新时间:2023-11-30 23:01:19 25 4
gpt4 key购买 nike

我最近被问到一个关于 .NET WebAPI 的面试问题,它是这样的......

面试官:

Is there any benefit to adding async to the method signature of a WebAPI controller action that does not perform any async operations?

我的回答:

No, because there is no blocking code in the method which means the controller action should run synchronously in its own thread from the thread Pool. Is that correct?

面试官:

No, it's actually not.

我在网上搜索了大约 15 到 20 分钟以找到结论性答案,但对答案并不完全满意...所以,我是对的还是面试官是对的?

为了进一步澄清这个问题:在这种情况下,负载测试 WebAPI Controller 操作是否会在负载下表现得更好,仅仅是因为它在签名中放置了一个异步关键字,即使它没有异步代码?

最佳答案

async 修饰符本身什么都不做。

来自docs :

If the method that the async keyword modifies doesn't contain an await expression or statement, the method executes synchronously. A compiler warning alerts you to any async methods that don't contain await statements, because that situation might indicate an error.

关于c# - 将异步放在不执行等待操作的 WebAPI Controller 操作上有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51091546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com