Answered by:
HttpWebRequest: async vs synchronous on Threadpool thread

Question
-
Hi there,
for use on vanilla Windows7 systems (.Net 4.0) I still use my own Http Library, which is built around the Httpwebrequest and includes synchronchronous and asynchronous functions for different mime-types, as well as JSON and XML support.
By now I use .BeginGetRequest and .BegitGetResponse for the async calls, but extending the library blows up the code and makes it more and more complex. As an alternative I have considered wrapping the existing sync versions transparently into a delegate wrapper and executing it on a threadpool thread (example below).
As I see it, advantages would be:
- simpler exception handling (around .endinvoke)
- more transparent and failsafe code
- simple implementation of Timeout and ReadTimeout (which is a bit of a pain in the async version)
- requests are created on the threadpool thread as well (slight ramp up)
Question: can you suggest any disadvantages going this way (performance, scaling, resources) ? Or a better way of doing it ?
A potential issue might be, that I am blocking a full threadpool thread, instead of using IO/blocking mechanisms.
Any answers / explanations / references for further reading would be greatly appreciated.
And yes, I'm using the Httpclient for the .Net 4.5 framework
Code examples:
''' <summary>Executes as synchronous GET request and returns string result</summary> Public Function GetSync(url As String) As String ' create GET request Dim request = DirectCast(Net.WebRequest.Create(url), HttpWebRequest) request.Method = "GET" ' fetch result Dim response = DirectCast(request.GetResponse(), HttpWebResponse) Using responsestream = response.GetResponseStream, reader = New IO.StreamReader(responsestream) Return reader.ReadToEnd() End Using End Function ''' <summary>Executes an async GET request and directs result to user defined callback</summary> Public Sub GetAsync(url As String, resultCallback As Action(Of String)) ' create GET request Dim request = DirectCast(Net.WebRequest.Create(url), HttpWebRequest) request.Method = "GET" ' send async request request.BeginGetResponse(New AsyncCallback( _ Sub(iar As IAsyncResult) 'Dim result = DirectCast(iar.AsyncState, AsyncResult) Using webResponse As HttpWebResponse = DirectCast(request.EndGetResponse(iar), HttpWebResponse), _ responsestream = webResponse.GetResponseStream, reader = New IO.StreamReader(responsestream) resultCallback(reader.ReadToEnd()) End Using End Sub), Nothing) End Sub ''' <summary>Executes a synchronous GET request on a threadpool thread and directs result to user defined callback</summary> Public Sub GetAsyncWrapped(url As String, resultCallback As Action(Of String)) ' define wrapper delegate Dim worker = New Action( _ Sub() Dim request = DirectCast(Net.WebRequest.Create(url), HttpWebRequest) request.Method = "GET" resultCallback(GetSync(url)) End Sub) ' run it async on a threadpool thread worker.BeginInvoke(New AsyncCallback( _ Sub(iar As IAsyncResult) worker.EndInvoke(DirectCast(iar, AsyncResult)) End Sub), Nothing) End Sub
To explain the question I used simple versions with lambda expressions.
- Edited by Frank Rossler Wednesday, September 25, 2013 1:25 PM
- Moved by Carl Cai Thursday, September 26, 2013 6:12 AM if post it in asp.net forum will get better support
Wednesday, September 25, 2013 1:17 PM
Answers
-
Hi,
Thank you for visiting the MSDN forum.
You are more likely to get more efficient responses to ASP.NET issues at http://forums.asp.net where you can contact ASP.NET experts, since this issue is most related to web.
Thanks for your understanding.
Regards.
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.Thursday, September 26, 2013 6:12 AM