IIS multithreading

I have a web application in IIS 7. There is a page with Button1 . When I click this Button1 , the following method starts:

 string url = "http://example.com"; string resultStr = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); string encoding = resp.CharacterSet; if (encoding.Equals(String.Empty)) encoding = "UTF-8"; Stream strResponse = resp.GetResponseStream(); int bytesSize = 0, c = 0; byte[] downBuffer = new byte[2048];//2kb while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) { if (++c > 100) break;//200kb - max downBuffer = Encoding.Convert(Encoding.GetEncoding(encoding), Encoding.UTF8, downBuffer); string tempString = Encoding.UTF8.GetString(downBuffer, 0, bytesSize); resultStr += tempString; } strResponse.Close(); TextBox1.Text = resultStr; 

As you can see, TextBox1 will contain the html code of the remote page.

There was a problem: while this method works, I cannot load other pages! How to solve this problem?

I know that there is an application pool that stores application threads, so the server can process multiple threads at the same time ... But this does not work for me. Why?

+4
source share
1 answer

I suspect you are using Session in your application, right? The session is not thread safe. This means that if you use Session, ASP.NET blocks the entire request and does not allow parallel requests to run ( from the same session ). Requests from one session are executed sequentially. You can have multiple requests from different sessions running in parallel.

This is actually a little more subtle than that. ASP.NET uses ReaderWriterLock to synchronize access to the session object, which means that you can have 2 requests from the same session that only read from it in parallel, but while you have write access to the session, it blocks other parallel requests from the same session.

You can control this by using EnableSessionState="ReadOnly" on your page to indicate that it is only a read from the session and thus the ability to execute queries in parallel.

+2
source

Source: https://habr.com/ru/post/1388713/


All Articles