views:

357

answers:

1

I am calling a RESTful web service in the back-end of some ASP.NET pages.

I am using ASP.NET asynchronous pages, so under the hood I am using the methods:

HttpWebRequest BeginGetResponse()

and

HttpWebRequest EndGetResponse()

The response string in my case is always a JSON string. I use the following code to read the entire string:

using (StreamReader sr = new StreamReader(myHttpWebResponse.GetResponseStream()))
{
  myObject.JSONData = sr.ReadToEnd();
}

Is this method OK in terms of scalability? I have seen other code samples that instead retrieve the response data in blocks using Read(). My primary goal is scalability, so this back-end call can be made across many concurrent page hits.

Thanks, Frank

A: 

It depends on what you mean by "scalable". If you're talking about being able to handle bigger and bigger files, I'd say it's not terribly scalable. Since you're using a single ReadToEnd, a huge stream would require the entire stream be read into memory and then acted upon. As the application streams grow in number, complexity and size you're going to find that this will begin to hamper the server's performance to handle requests. You may also find that your application pool will begin to recycle itself DURING your request (if you end up taking that much virtual memory).

If the stream is always going to be smallish and you're only concerned with the number of streams created, I don't see why this wouldn't scale as long as your streams were dependent on open files, database connections, etc.

Joel Etherton
Handling large files is not an issue - the JSON responses will generally be in the range of 1-10 kilobytes.I am referring to scalability in the sense that it can handle many concurrent requests.
frankadelic
I'd say keep your streams independent of any resource hogging allocation code -- basically any secondary stream or connection object and it should scale along with your application pool. It'll boil down to CPU/Memory/Network capability. Keeping the code properly scoped (as your mini-example is) should allow an enormous amount of simultaneous connections asynchronously.
Joel Etherton
Are you making the HTTP request to the same backend server from your ASPX app? If so, you will need to bump up the max connection limit in the WebRequest settings. Otherwise,even though your IIS server is servicing a huge number of clients, the pages will be throttled on the number of outgoing connections available for HTTPWebRequest.You can set the limit as follows:ServicePointManager.DefaultConnectionLimit = 100; // for eg;
feroze
@feroze - In this case, I am calling a 3rd-party backend server. Is there a drawback to setting DefaultConnectionLimit to a very high value?
frankadelic