Enabling concurrent requests from a single session in ASP.NET Core

EDIT: This appears to be an issue with testing in the browser, not with the code. Core sessions are disabled by default, as they should.

Original question:

I am working on a web API that needs to handle multiple requests at the same time, even from the same client. This is an ASP.NET Core MVC (1.1.2) app currently targeting the full framework (4.5.2), mainly for compatibility with other libraries.

When I first checked my concurrency, I was surprised that the requests were not concurrent. I googled around and found out that ASP.NET does not process requests for a session at the same time, but enqueues them instead. A quick test reveals that sessions are likely to be the culprit:

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

      

When I quickly request this from multiple tabs in the same browser, it takes 5 seconds between each tab. This is not the case when I use multiple browsers, it reacts multiple times within 5 seconds.

While looking for a solution, I kept on dwelling on ways to disable session state in ASP.NET, but nothing for Core.

In terms of sessions, I am using the default API project template and I have not done anything to specifically enable / configure session state.

Is there a way to get rid of session state in ASP.NET Core? Or is there a better solution for allowing concurrent requests?

+6


source to share


2 answers


You already have parallel requests enabled in ASP.NET Core, no need to change your code. Session in ASP.NET Core is not blocked. If multiple requests modify the session, the last action will be won.

As the documentation states :



The session state is non-blocking. If two requests simultaneously try to modify the contents of the session, the last request cancels the first. The session is implemented as a consistent session, which means all content is kept together. When two requests try to change different session values, the last request can override the session changes made by the first.

+1


source


If you set this attribute in your controller class

[SessionState(SessionStateBehavior.ReadOnly)]



This will establish a session so as not to block, and therefore you will be able to make parallel requests

You can read more about it here

0


source







All Articles