Async WebRequest isn't asynchronous?



I have developed a simple webrequest method that will call a remote service from one of my web pages. The call is part of a class in a separate
assembly. The web request code is a pretty basic async call:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(EndPointURL);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = data.Length;
Stream strm = webRequest.GetRequestStream();
strm.Write(data, 0, data.Length);

// For Async Call
// Create the state object.
RequestState rs = new RequestState();

// Put the request into the state object so it can be passed around.
rs.Request = webRequest;

// Issue the async request.
IAsyncResult r = (IAsyncResult)webRequest.BeginGetResponse(new AsyncCallback(RespCallback), rs);

ThreadPool.RegisterWaitForSingleObject(r.AsyncWaitHandle, new WaitOrTimerCallback(ScanTimeoutCallback), rs, (30 * 1000), true);

The problem is that it doesn't appear to be asynchronous at all. The page will not finish loading until the web request completes. A couple of
things don't make sense here first is the RequestState object (this is code straight from the documentation). I have no idea what this is doing or
what it is used for and since it appears to be required why do I have to create it as a custom class instead of there being RequestState class in the
framework already. Seems pretty absurd that there is a required object interface that is not even defined in the framework. The next thing is of
course why is the page hanging until the request completes? The code above should immediately return control to the calling page and allow the
callback handler to handle the response?


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question