This article is a mirror article of machine translation, please click here to jump to the original article.

View: 29669|Reply: 3

[ASP.NET] ASP.NET multiple requests on the page at the same time, there is a blocking suspended animation situation

[Copy link]
Posted on 9/22/2017 5:32:59 PM | | | |
Recently, when I was working on a project, some pages needed to load a lot of data, and sometimes, I clicked on the page without waiting for the page to finish loading, and then clicked on another page again

There will be a very slow state of suspended animation while loading the web page, so let's study it carefully today.

At first, I thought that this situation would occur on many websites or my computer network speed problem, but I found that this site did not have this situation, sometimes, I would get stuck when I posted, but I clicked on other pages in the tab to load quickly.

Let's take a closer look today!! The code tested first:

Homeview Code:

Controller Code:



For test code analysis, our controller has 3 methods, one is the home page, and the other two are test methods

The Test1 request blocks for 5 seconds and then returns data to the user

Test2 requests will not block and will return data directly to the user

Our homepage is two interfaces for Ajax requests, which are asynchronous requests, so there is no blocking problem.

We will find that Test1 method outputs content only after Test2 outputs content (Normally, the page will output the content returned by Test2 directly, and then wait 5 seconds to output the content returned by Test1, because js does not block



Then, we directly access the Test1 and Test2 interfaces, we access Test1 first, and then immediately access Test2, and find that Test2 must wait until Test1 returns to complete, as shown in the figure below:



If a page request sets a reader lock, other requests that are being processed at the same time in the same session will not be able to update the session state, but at least they can be read. If a page requests a write lock for the session state, then all other pages are blocked, regardless of whether they want to read or write content. For example, if two program views are writing content in the same session at the same time, one program must wait until the other program is finished before it can be written. In AJAX programming, it is important to be aware of this happening.

Special note: Only when writing a session, Asp.net will block the request, but as long as you have visited the page where the session is written, such as the operation after logging in to the system with the session (the session is locked until it expires, of course, it is only the case that the SessionID is the same). There will be this problem.

Netizen information

As long as the website uses a session, each request will lock the session throughout its lifetime, so that requests with the same sessionid must wait to be unlocked

This means that if the website has a timed out page, it can't do anything, and you have to wait for the timed page to load.

You can't do it either, multiple ajax concurrent requests on the same page, you can't do it, message polling requests.



To sum it up:If you take a session to the request, if you do not bring a session to the request, the above situation will not occur

Solution:

Added the SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly) feature to the controller controller

Note:

Required means you are requesting a exclusive lock on Session (i.e. no parallel processing of requests for the same sessionID)
ReadOnly means you are requesting a non-exclusive lock on Session (i.e. your request still has to wait for requests with an exclusive lock to finish, but you can process requests with non-exclusive locks in parallel. However it is up to you to ensure that your code doesn't write to Session. It's not necessarily enforced by the framework)
Required means the session mutex you requested (i.e. there is no requirement to process the same SessionID in parallel)

ReadOnly means that the session you request is a non-exclusive lock (i.e. your request still has to wait for completion, the request for an exclusive lock but you can process a request with a parallel non-exclusive lock). But you want to make sure your code doesn't write sessions. It doesn't have to be executed by the framework)





Previous:CEF: Embed a Chrome for the client
Next:HTTP Error 503. The service is unavailable.
Posted on 7/6/2018 11:53:31 AM |
For WebForms, add after the Page at the top of aspx (just load that blocking page):

EnableSessionState="ReadOnly"
 Landlord| Posted on 7/14/2019 8:34:17 PM |
Most web development uses sessions to save session states, but using sessions in asp.net applications may have a significant impact on the performance of web applications. Why and what will affect it is analyzed below

MSDN explanation of concurrent requests and session states:

Concurrent requests and session status
Access to the ASP.NET session state is per-session, meaning that if two different users send a request at the same time, access to each individual session is granted at the same time. However, if the two concurrent requests are for the same session (by using the same SessionID value), the first request will gain exclusive access to the session information. The second request will only be executed after the first request is completed. (If the exclusive lock on the session information is released because the first request exceeds the lock timeout, the second session also gains access.) If you set the EnableSessionState value in the @Page directive to ReadOnly, requests for read-only session information do not result in an exclusive lock on the session data. However, read-only requests to session data may still need to wait until the lock is unlocked from the read/write request set by the session data.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com