C#httplistener begingetcontext不会使用msdn示例调用回调 [英] C# httplistener begingetcontext wont call callback using msdn example

查看:780
本文介绍了C#httplistener begingetcontext不会使用msdn示例调用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上在这里运行示例:
http://msdn.microsoft.com/en-us/library/system.net.httplistener.begingetcontext.aspx

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();

        }
    }
}

RawUrl没有打印,msg也是空白。我在回调函数中放置的断点甚至不会停止执行

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

推荐答案

知道了,问题是监听器。关闭()是当WaitOne()返回时正在执行的主函数,所以在ListenerCallback中它说资源已经被处理掉了。

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.

此处还需要回调结束
IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback),listener);

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

接受下一个请求

这篇关于C#httplistener begingetcontext不会使用msdn示例调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆