Pending HttpListener requests: multiple and terminating

V

valentin tihomirov

Hi,

[Issue 1]
How do you process multiple requests by HttpListener? I cannot find the
intended method of starting multiple pending BeginContext simultaneously.
The only home-made example I have discovered is
http://west-wind.com/weblog/posts/3748.aspx, which suggests starting a new
accept only after the previous one is received in the request handler:

protected void WebRequestCallback(IAsyncResult result)
{
if (this.Listener == null)
return;

// Get out the context object
HttpListenerContext context =
this.Listener.EndGetContext(result);

// *** Immediately set up the next context
this.Listener.BeginGetContext(new
AsyncCallback(WebRequestCallback), this.Listener);

IMO, it is nonsense to allow only one asynchronous operation at a time. The
very notion of asynchrony, its main feature and advantage is that you can
start many operations simultaneously, even on the same device object. This
not only saves resources by avoiding entailing of useless threads, but also
provides performance advantages by avoiding device standstills. For
instance, when writes are asynchronous, you can start two at once providing
two buffers and fill the first buffer completed, effectively doing double
buffering. Obviously, the new requests will wait in the backlog until the
previous starts being processed in the multiconnection method used in the
example. What is the reason/need for this waitings?

Actually, this is not a big issure, since the performance penalty is minimal
and we can live with that. But we can not leave the application without
closing the resourses and outstanding requests. Therefore, the issue 2 is
extremely critical to resolve.


[Issue 2]
The official manual tells that HttpListener.Close() "Shuts down the
HttpListener after processing all currently queued requests." But
this is a lie! I have one pending GetContext request and print a log message
in the request completion callback routine. The callback is not invoked when
application exits after closing the server. I felt that the outstanding
request should complete with fail when server is closed and injected a sleep
after the HttpListener.Close() to give the time for the request to complete
before the app exits. And the callback was called! It fails at EndGetContext
with ObjectDisposed exception.


So, the questions in breif are: 1) what is the proper way to start multiple
asynchronous GetContext requests and 2) wait for them to complete before
closing the server?
 

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

Top