Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Original question:

I'm working on a web API which needs to handle multiple requests concurrently, even from the same client. 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.

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. 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. This is not the case when I use multiple browsers, it responds multiple times within a 5 second window.

When searching for a solution I kept stumbling upon ways to disable session state in ASP.NET, but nothing for 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.

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

like image 663
Michal S Avatar asked Jun 13 '17 16:06

Michal S


People also ask

How does ASP NET core handle multiple requests?

ASP.NET Core apps should be designed to process many requests simultaneously. Asynchronous APIs allow a small pool of threads to handle thousands of concurrent requests by not waiting on blocking calls. Rather than waiting on a long-running synchronous task to complete, the thread can work on another request.

How does ASP net handle concurrent requests?

Concurrent requests simply means that responding multiple requests by a web server simultaneously. In simple words you can say that a server is responding multiple requests without waiting for a request to complete. ASP.NET doing the same task using CLR Thread Pool.

What methods are used to enable session in asp net core?

To use session in our Application, we need to add this package as a dependency in project. json file. The next step is to configure session in Startup class. We need to call "AddSession" method in ConfigureServices method of startup class.

What are concurrent HTTP requests?

Concurrent HTTP refers to HTTP requests made at any point in time. For example, let's say there are 10000 users with valid sessions and 100 users are requesting to read the same resource over HTTP at any point in time then we have 100 concurrent HTTP requests.


2 Answers

You already have concurrent requests enabled in ASP.NET Core, no need to modify your code. Session in ASP.NET Core is non-locking. 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.

like image 192
Dmitry Bogatykh Avatar answered Oct 21 '22 21:10

Dmitry Bogatykh


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

like image 1
Lucian Popescu Avatar answered Oct 21 '22 22:10

Lucian Popescu