Introduction
I would like to share a way for a server to transfer the data to the client in chunks rather than sending all the data at once.
This approach is useful when we have a large amount of data in the server and the client is waiting for an entire page to be displayed. Since buffering is enabled by default in ASP .Net, all the output from your ASP is actually sent to the browser only when the page completes its processing.
Objective
In this article we will learn the following:
Problem Statement
The client might need to wait until all the processing is completed on the server, because the server will send a response back to the client only when the page completes its processing.
- If the server is taking a lot of time to process the request then the Client must wait a long time and that is not a good approach.
- Servers need to retain a huge amount of data in memory.
Solution
Now as know, by default buffering is enabled so the client must wait until all the processing is done by the server.
We can use Response.flush to send the buffered output to the client.
The Response.flush method is used to send buffered output immediately when the Response.Buffer is set to true.
By default it is Response.Buffer= true.
Consider the following code:
- for (int i = 0; i < 100; i++)
- {
- Thread.Sleep(1000);
- Response.Write(i + "\n");
- }
Here in this code the client needs to wait for 100 sec because Thread.Sleep(1000)= 1 sec and we loop for 0 to 99. So in the code above the Client must wait and that is not good.
Using Response.flush
- for (int i = 0; i < 100; i++)
- {
- Thread.Sleep(1000);
- Response.Write(i + "\n");
- Response.Flush()
- }
Here from the code above, the Client does not need to wait for 100 sec, the client will get the output on each and every second.
Data will be transferred to the client in chunks. We would see the updated screen after each and every second.
When I debug the Httprequest and Httpresponse, I discover that the data has been transferred in chunks.
The following is the snapshot of solution approach above. Here we can see that Transfer-encoding : Chunked.
Other approach
We can also use Response.Bufferoutput =false to get the data in chunks. For more information on response.buffer kindly visit the following link :
Improving ASP.Net Performance Using Response.BufferOutput
For more on HTTP debugging, kindly see the following link:
Debugging HTTP Requests and HTTP Responses
Conclusion
We can improve the performance of ASP.Net applications using this technique.
References:
Chapter 6 - Improving ASP.NET Performance