C# httplistener begingetcontext wont call callback

2019-09-18 03:15发布

I'm basically running the example here: http://msdn.microsoft.com/en-us/library/system.net.httplistener.begingetcontext.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Servertest
{
    class Program
    {
        static string msg;
        public static void Main(string[] prefixes)
        {
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
                Console.ReadLine();
                return;
            }
            // URI prefixes are required,
            // for example "http://contoso.com:8080/index/".

            //if (prefixes == null || prefixes.Length == 0)
              //  throw new ArgumentException("prefixes");

            // Create a listener.
            HttpListener listener = new HttpListener();
            // Add the prefixes.
            //foreach (string s in prefixes)
            //{
            listener.Prefixes.Add("http://*:2999/");
            //}
            listener.Start();
            Console.WriteLine("Listening...");
            // Note: The GetContext method blocks while waiting for a request.
            IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
            // Applications can do some work here while waiting for the 
            // request. If no work can be done until you have processed a request,
            // use a wait handle to prevent this thread from terminating
            // while the asynchronous operation completes.
            Console.WriteLine("Waiting for request to be processed asyncronously.");
            result.AsyncWaitHandle.WaitOne();
            Console.WriteLine("Request processed asyncronously.");
            Console.WriteLine(msg);
            listener.Close();
        }
        public static void ListenerCallback(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;
            // Call EndGetContext to complete the asynchronous operation.
            HttpListenerContext context = listener.EndGetContext(result);
            HttpListenerRequest request = context.Request;
            // Obtain a response object.
            //Break point here doesnt even get stopped
            //Console.WriteLine(request.RawUrl) doesn't show anything either
            msg = new string(request.RawUrl.ToCharArray());
            HttpListenerResponse response = context.Response;
            // Construct a response.
            string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            // Get a response stream and write the response to it.
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            // You must close the output stream.
            output.Close();

        }
    }
}

The RawUrl doesnt get printed, and msg is blank too. Break points I put in the callback function don't even stop the execution

1条回答
forever°为你锁心
2楼-- · 2019-09-18 04:02

Got it, the issue was the listener.Close() is the main function which was being executed right when WaitOne() returned so in the ListenerCallback it was saying the resource was already disposed of.

Also needed this at the end of the callback IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);

to accept next request

查看更多
登录 后发表回答