简体   繁体   English

在ASP.NET Core中启用来自同一会话的并发请求

[英]Enabling concurrent requests from same session in ASP.NET Core

EDIT: This was apparently an issue with testing in the browser, not with the code. 编辑:这显然是在浏览器中测试的问题,而不是代码。 Sessions in Core are disabled by default as they should be. 默认情况下,Core中的会话将被禁用。

Original question: 原始问题:

I'm working on a web API which needs to handle multiple requests concurrently, even from the same client. 我正在开发一个Web API,它需要同时处理多个请求,即使是来自同一个客户端。 It is an ASP.NET Core MVC (1.1.2) app currently targeting the full framework (4.5.2), mostly for compatibility with other libraries. 它是一个ASP.NET核心MVC(1.1.2)应用程序,目前针对完整框架(4.5.2),主要是为了与其他库兼容。

When I first tested my concurrency, I was surprised that the requests were not concurrent at all. 当我第一次测试我的并发性时,我很惊讶这些请求根本不是并发的。 I googled around and found out that ASP.NET does not handle requests within a session concurrently, but instead queues them. 我用Google搜索并发现ASP.NET不会同时处理会话中的请求,而是将它们排队。 A quick test shows that sessions are likely to be the culprit: 快速测试显示会话可能是罪魁祸首:

[HttpGet("sleep")]
public string Sleep()
{
   Thread.Sleep(5000);
   return $"Done at {DateTime.Now:u}";
}

When I rapidly request this from multiple tabs in the same browser, it takes 5 seconds between each tab. 当我从同一浏览器中的多个选项卡快速请求时,每个选项卡之间需要5秒钟。 This is not the case when I use multiple browsers, it responds multiple times within a 5 second window. 当我使用多个浏览器时,情况并非如此,它会在5秒钟内响应多次。

When searching for a solution I kept stumbling upon ways to disable session state in ASP.NET, but nothing for Core. 在搜索解决方案时,我一直在寻找在ASP.NET中禁用会话状态的方法,但对Core来说却没有。

As far as sessions are concerned, I am using the default API project template and I have done nothing to specifically enable/setup session state. 就会话而言,我使用的是默认的API项目模板,而我没有采取任何措施来专门启用/设置会话状态。

Is there a way to get rid of session state in ASP.NET Core? 有没有办法摆脱ASP.NET Core中的会话状态? Or is there a better solution to enable concurrent requests? 或者是否有更好的解决方案来启用并发请求?

You already have concurrent requests enabled in ASP.NET Core, no need to modify your code. 您已经在ASP.NET Core中启用了并发请求,无需修改代码。 Session in ASP.NET Core is non-locking. ASP.NET Core中的会话是非锁定的。 If multiple requests modify the session, the last action will win. 如果多个请求修改了会话,则最后一个操作将获胜。

As-stated in the documentation : 如文件中所述

Session state is non-locking. 会话状态是非锁定的。 If two requests simultaneously attempt to modify the contents of a session, the last request overrides the first. 如果两个请求同时尝试修改会话的内容,则最后一个请求将覆盖第一个请求。 Session is implemented as a coherent session, which means that all the contents are stored together. 会话实现为连贯会话,这意味着所有内容都存储在一起。 When two requests seek to modify different session values, the last request may override session changes made by the first. 当两个请求试图修改不同的会话值时,最后一个请求可能会覆盖第一个请求所做的会话更改。

If you set this attribute on your controller class 如果在控制器类上设置此属性

[SessionState(SessionStateBehavior.ReadOnly)]

It will set the session to not lock and therefore you will be able to make concurrent requests 它会将会话设置为不锁定,因此您将能够进行并发请求

You can read more about it here 你可以在这里阅读更多相关信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 ASP.NET Core REST API 并发请求后来自 MongoDb 的响应缓慢 - Slow responses from MongoDb after concurrent requests via ASP.NET Core REST API HttpClient 请求无法识别 session ASP.NET 来自 razor 的 Core 6 MVC - HttpClient requests not recognizing session ASP.NET Core 6 MVC from razor 在ASP.NET MVC中启用会话状态 - Enabling Session State in ASP.NET MVC 并发Web API请求以及如何处理ASP.NET Core中的状态 - Concurrent web api requests and how to handle state in ASP.NET core 使用ASP.NET,如何强制IIS 7从同一台计算机给我两个并发会话ID? - Using ASP.NET, how can I force IIS 7 to give me two concurrent session IDs from the same computer? ASP.NET 核心MVC session 同站 URL - ASP.NET Core MVC session same site URL 在 ASP.NET 核心 MVC 中从 controller 添加 session 超时 - Add session timeout from controller in ASP.NET Core MVC 2个ASP.NET请求之间的共享会话 - Sharing Session between 2 ASP.NET Requests 使用 Entity Framework Core 更新 ASP.NET Core 中相同字段的并行请求 - Parallel requests to update the same field in ASP.NET Core using Entity Framework Core ASP.NET中使用线程的多个并发Web服务请求 - Multiple concurrent webservice requests in ASP.NET using threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM