C# problem with HttpListener
<p>I wrote a Windows service using an <code>HttpListener</code> to process the requests from points asynchronously.</p>

<p>It works fine, but sometimes it runs into a problem that requires restarting the service or server to fix. Initially I declared the listener object with:</p>

<pre><code>public HttpListener PointsListener = new HttpListener();
</code></pre>

<p>Here is the code of the method, where I starting listening. I'm calling it from the <code>OnStart</code> method of the service:</p>

<pre><code>    public string ListenerStart()
    {
        try
        {
            if (!PointsListener.IsListening)
            {
                PointsListener.Prefixes.Add(String.Concat("http://*:", points_port, "/"));
                PointsListener.Start();
                PointsListener.BeginGetContext(PointProcessRequest, PointsListener);

                LogWriter("Http listener activated on port " + points_port);
                return "Listener started";
            }
            else
            {
                return "Listener is already started!";
            }

        }
        catch (Exception err)
        {
            LogWriter("Error in LIstenerStart \r\n" + err.ToString());
            return ("Error: " + err.Message);
        }
    }
</code></pre>

<p>Here are the methods which process requests:</p>

<pre><code>    private void PointProcessRequest(IAsyncResult result)
    {
        HttpListener listener = (HttpListener)result.AsyncState;
        HttpListenerContext context = listener.EndGetContext(result);
        HttpListenerRequest request = context.Request;
        HttpListenerResponse response = context.Response;
        response.KeepAlive = false;
        System.IO.Stream output = response.OutputStream;

        try
        {
            //declaring a variable for responce
            string responseString = "&lt;html&gt;My Response: request is not allowed by server protocol&lt;/html&gt;";


            // Commands and actions to set responceString

            byte[] buffer = Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64 = buffer.Length;
            output.Write(buffer, 0, buffer.Length);
        }
        catch (Exception err)
        {
            LogWriter("Error in PointProcessRequest: \r\n" + err.ToString());
        }
        finally
        {
            try
            {
                output.Flush();
                output.Close();
                response.Close();
            }
            catch (Exception err)
            {
                LogWriter("Error in PointProcessRequest CLOSING OUTPUT STREAM: \r\n" + err.ToString());
            }
            finally
            {
                PointsListener.BeginGetContext(PointProcessRequest, PointsListener);
            }
        }
    }
</code></pre>

<p>It is working well some of the time, but the following error appears in the log:</p>

<pre><code>Error in PointProcessRequest: 
System.Net.HttpListenerException: The specified network name is no longer available
   в System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   в ARK_Dealer.ark.PointProcessRequest(IAsyncResult result)

[26.01.2011 9:00:54] Error in PointProcessRequest CLOSING OUTPUT STREAM: 
System.InvalidOperationException: Cannot close stream until all bytes are written.
   в System.Net.HttpResponseStream.Dispose(Boolean disposing)
   в System.IO.Stream.Close()
   в ARK_Dealer.ark.PointProcessRequest(IAsyncResult result)
</code></pre>

<p>I think that problem appears when some point sends the request to server but before the receiving answer looses the connection.</p>

<p>How can I prevent the exception from being thrown? Will be the response object be properly disposed of automatically? How can I solve the problem?</p>
