John, thanks for the responses. I'd like to get this to work and then do some
load testing. I'm having trouble using the WaitAll(). I guess I thought it
would wait for all handles to respond but the "Done" if else block gets fired
before the responses return.
I'm concatenating the responses in in a textarea to display all the results.
Maybe my understanding of this is off...
HttpWebRequest wreq;
IAsyncResult r0;
IAsyncResult r1;
IAsyncResult r2;
WaitHandle[] handleArray;
handleArray = new WaitHandle[3];
wreq = (HttpWebRequest) WebRequest.Create("http://somefeed0");
r0 = (IAsyncResult) wreq.BeginGetResponse(new
AsyncCallback(this.RespCallback), wreq);
handleArray[0] = r0.AsyncWaitHandle;
wreq = (HttpWebRequest) WebRequest.Create("http://somefeed1");;
r1 = (IAsyncResult) wreq.BeginGetResponse(new
AsyncCallback(this.RespCallback), wreq);
handleArray[1] = r1.AsyncWaitHandle;
wreq = (HttpWebRequest) WebRequest.Create("http://somefeed2");
r2 = (IAsyncResult) wreq.BeginGetResponse(new
AsyncCallback(this.RespCallback), wreq);
handleArray[2] = r2.AsyncWaitHandle;
if(WaitHandle.WaitAll(handleArray, 30000, false))
{
Response.Write("Done");
}
else
{
// The wait operation times out.
Response.Write("Timeout");
}
}
private void RespCallback(IAsyncResult ar)
{
HttpWebRequest req = (HttpWebRequest) ar.AsyncState;
HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse(ar);
//Wrap the response stream with a text-based reader
StreamReader sr = new StreamReader(resp.GetResponseStream(),
System.Text.Encoding.UTF8);
string strStream = sr.ReadToEnd();
// Close the response to free resources.
sr.Close();
resp.Close();
txtHTML.InnerHtml += strStream;
}
[quoted text, click to view] "john conwell" wrote:
> You should definitly not do the sleep. What if the requests take too long,
> and 30 isnt long enough?
>
> Generally speaking, when doing multiple async web service requests, take the
> IAsyncResult.AsyncWaitHandle from each web service call and slap then into an
> array. then call WaitHandle.WaitAll() passing in the array of
> AsyncWaitHandles. This will block the current thread until all the web
> service calls have completed.
>
> Now, a few things to think about. Is your web site usually under a heavy
> load (high number of users)? If so, then kicking off 3 async web services per
> request to this page might be a bad idea, because you'll be stealing 3
> threads from the thread pool used to service other requests. this doesent
> scale very well at all.
>
> If you really want to do the async web service calls, another technique is
> to create a IHttpHandler to intercept your request, kick off the three web
> service calls and store the handle array in HttpContext.Items hashtable. The
> forward the request onto the correct page and in the page do the
> WaitHandle.WaitAll(). this gives the request a few extra moments to process
> the web service calls before they have to sync back up again.
>
> hot this helps
>
> "Dave" wrote:
>
> > Sorry for cross-posting but wasn't getting any response in the asp.net forum.
> >
> > I've have several webrequests being called sequentially in an .aspx that
> > return XML from sources outside the company. When the page runs it can take
> > anywhere between 45-60 seconds because each webrequest waits for the last
> > one to complete.
> >
> > I heard about making webrequests asynchronosly and found:
> >
> >
http://samples.gotdotnet.com/quickstart/howto/doc/ASPXNet/GETAsync.aspx > >
> > I modified the code to add a couple requests right after each other as
> > listed below.
> >
> > It worked but...
> >
> > 1.) What is the drawback of this technique in terms of performance in the
> > long run?
> > 2.) Is there a way to determine when all of these webrequests complete and
> > just flush the response instead of waiting for the Thread.Sleep to complete?
> >
> > HttpWebRequest wreq;
> > IAsyncResult r;
> >
> > wreq = (HttpWebRequest) WebRequest.Create("http://somexmlsource1");
> > r = (IAsyncResult) wreq.BeginGetResponse(new
> > AsyncCallback(this.RespCallback), wreq);
> >
> > wreq = (HttpWebRequest) WebRequest.Create("http://somexmlsource2");
> > r = (IAsyncResult) wreq.BeginGetResponse(new
> > AsyncCallback(this.RespCallback), wreq);
> >
> > wreq = (HttpWebRequest) WebRequest.Create("http://somexmlsource3");
> > r = (IAsyncResult) wreq.BeginGetResponse(new
> > AsyncCallback(this.RespCallback), wreq);
> >
> > Thread.Sleep(30000); <!--Wait 30 seconds to allow requests to complete