为什么这个 web api 控制器不是并发的? [英] Why is this web api controller not concurrent?

查看:34
本文介绍了为什么这个 web api 控制器不是并发的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web API 控制器,其中包含以下方法:

public string Tester(){线程睡眠(2000);返回确定";}

当我调用它 10 次时(使用 Fiddler),我希望所有 10 次调用都在大约 2 秒后返回.然而,调用分别在 2,4,8...20 秒后返回.是什么阻止它同时运行?我该如何解决?

常规控制器的行为是否与 web-api 控制器相同?

解决方案

您描述的内容与 ASP.NET 会话状态的默认行为相匹配,可以通过在 web.config 中禁用它来解决.

<块引用>

对 ASP.NET 会话状态的访问是每个会话独占的,这意味着如果两个不同的用户并发请求,则同时授予对每个单独会话的访问权限.但是,如果为同一个会话发出两个并发请求(通过使用相同的 SessionID 值),第一个请求将获得对会话信息的独占访问权.第二个请求仅在第一个请求完成后执行.(如果由于第一个请求超过了锁超时而释放了对信息的排他锁,则第二个会话也可以获得访问权限.)如果@Page 指令中的EnableSessionState 值设置为ReadOnly,则请求为只读会话信息不会导致对会话数据的排他锁.但是,对会话数据的只读请求可能仍需要等待由对会话数据的读写请求设置的锁才能清除.

来源:ASP.NET 会话状态概述>

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

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

    return "OK";
}

When I call it 10 times (Using Fiddler), I expect all 10 calls to return after ~ 2 seconds. However the calls return after 2,4,8...20 seconds, respectively. What is blocking it from running concurrently? How do I fix that?

Do regular Controllers behave the same as web-api controllers?

解决方案

What you describe matches the default behavior of the ASP.NET Session State and can be solved by disabling it in web.config.

Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the EnableSessionState value in the @ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear.

Source: ASP.NET Session State Overview

这篇关于为什么这个 web api 控制器不是并发的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆