With ASP.NET 4 – Microsoft has introduced the ability to read an HTTP request entity as a stream using the HttpRequest.GetBufferlessInputStream method. This method provides streaming access to the request entity. But this process happens synchronously because it was tied to a thread until the request is completed.

Now with new ASP.NET 4.5 – Microsoft has introduced  the ability to read streams asynchronously on an HTTP request entity using the HttpRequest.GetBuffererdInputStream, and developers will be able to programmatically flush the request buffer asynchronously. ASP.NET 4.5 also provides you the ability to double-buffer an HTTP request entity, which provides easier integration with downstream HTTP handlers such as .aspx page handlers and ASP.NET MVC controllers.

HttpRequest Handling

The Stream reference returned by ASP.NET 4.5 from HttpRequest.GetBufferlessInputStream supports both synchronous and asynchronous read methods. The Stream object returned from GetBufferlessInputStream now implements both the BeginRead and EndRead methods. The asynchronous Stream methods let you asynchronously read the request entity as small chunks, while ASP.NET releases the current thread between each iteration of an asynchronous read loop.

The new helper method provides the capability for the developer to read the request entity in a buffered way using HttpRequest.GetBufferedInputStream. GetBufferedInputStream works like GetBufferlessInputStream, supporting both synchronous and asynchronous read operations. While doing read operation, GetBufferedInputStream also copies associated data bytes into ASP.NET buffer system so that ongoing modules and handlers can still access the request entity, and retrieve/extract necessary information from the request entity.

Asynchronously flushing a response

Sending responses to an HTTP client can take considerable time when the client is far away or has a low-bandwidth connection.

  • Normally ASP.NET buffers the response bytes as they are created by an application.
  • ASP.NET then performs a single send operation of the accrued buffers at the very end of request processing.

If the buffered response is large (for example, streaming a large file to a client), you must periodically call HttpResponse.Flush to send buffered output to the client and keep memory usage under control. However, because Flush is a synchronous call, iteratively calling Flush still consumes a thread for the duration of potentially long-running requests.

Now we can perform flushes asynchronously using the BeginFlush and EndFlush methods of the HttpResponse class.

  • Using these methods, you can create asynchronous modules and asynchronous handlers that incrementally send data to a client without tying up operating-system threads.
  • In between BeginFlush and EndFlush calls, ASP.NET releases the current thread.
  • This substantially reduces the total number of active threads that are needed in order to support long-running HTTP downloads.

Very nice feature isn’t it? ASP.NET is getting lots of improvements from time to time and these features are really good when we work with AJAX requests, and also build a highly responsive web application. 

Thanks for reading and Information courtesy MSDN and ASP.NET website.  You want to know more about What is new about ASP.NET 4.5? visit here


Discover more from Cloud Distilled ~ Nithin Mohan

Subscribe to get the latest posts sent to your email.

By Nithin Mohan TK

Technology Enthusiast | .NET Specialist | Blogger | Gadget & Hardware Geek

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.