Pending HttpListener requests: multiple and terminating RRS feed

  • Question

  • Hi,

    I have a couple of related issues.

    [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 preceding one is received in the request handler:

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

                // 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 one can start many operations simultaneously, even on the same device object. This not only saves computer resources by avoiding entailing of useless threads, but also provides performance advantages by avoiding device standstills. For instance, when one uses asynchronous writes, he can start two writes at once providing two buffers. He then fills the first buffer completed with new data and submits it again, effectively doing double buffering. Obviously, the new HttpListener requests will wait in the backlog until processing of the current request starts in the multiconnection method used in the example above. What is the reason/need for this waiting?

    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 opened resources 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. Feeling that the outstanding request should complete with fail when server is closed, I have injected a sleep after the HttpListener.Close(). This give time for the request to complete before the app exits and the callback was called! It fails at EndGetContext with ObjectDisposed exception.

    I have to know a way to cancel the pending GetContext requests and wait until the processing of the requests completes.

    So, the questions in breif are: what is the proper way to
        1) start multiple asynchronous GetContext requests; and
        2) wait until they have processes before closing the server?

    Thursday, April 19, 2007 1:58 PM

All replies

  • Nobody knows how to terminate an HttpServer properly?
    Monday, May 7, 2007 12:28 PM