干净interupt的HttpListener的BeginGetContext方法干净、方法、HttpListener、interupt

由网友(那愛已痛心凱)分享简介:我使用的是的HttpListener 和使用 BeginGetContext 让我的上下文对象。我想彻底关闭我的HttpListener但如果我试图做一个的在我得到一个例外,听者关闭它会导致我的程序退出。I am using a HttpListener and using BeginGetContext to ge...

我使用的是的HttpListener 和使用 BeginGetContext 让我的上下文对象。我想彻底关闭我的HttpListener但如果我试图做一个的在我得到一个例外,听者关闭它会导致我的程序退出。

I am using a HttpListener and using BeginGetContext to get my context object. I want to cleanly shut down my HttpListener but if I attempt to do a Close on the listener I get a exception and it causes my program to exit.

using System;
using System.Net;

namespace Sandbox_Console
{
    class Program
    {
        public static void Main()
        {
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
                return;
            }

            // Create a listener.
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://vwdev.vw.local:8081/BitsTest/");
            listener.Start();
            Console.WriteLine("Listening...");

            listener.BeginGetContext(Context, listener);
            Console.ReadLine();

            listener.Close(); //Exception on this line, but not shown in Visual Studio
            Console.WriteLine("Stopped Listening..."); //This line is never reached.
            Console.ReadLine();

        }

        static void Context(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;
            HttpListenerContext context = listener.EndGetContext(result);

            context.Response.Close();
            listener.BeginGetContext(Context, listener);
        }
    }
}

该计划抛出一个异常 listener.Close()但是永远不会被显示在Visual Studio中的错误,唯一注意我得到的是在调试输出以下屏幕:

The program throws a exception on the listener.Close() however the error never gets shown in Visual Studio, the only note I get is the following in the Debug output screen:

有一个第一次机会异常的类型'System.ObjectDisposedException在System.dll中发生   该方案[2568]沙盒Console.vshost.exe:托管(v4.0.30319)'。已退出与code 0(为0x0)

A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll The program '[2568] Sandbox Console.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

我能够从Windows事件查看器

I was able to get the real execption from windows Event Viewer


Application: Sandbox Console.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException
Stack:
   at System.Net.HttpListener.EndGetContext(System.IAsyncResult)
   at Sandbox_Console.Program.Context(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ListenerAsyncResult.WaitCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

什么我需要做的,所以我可以清晰地阖上的HttpListener?

What do I need to do so I can close my HttpListener cleanly?

推荐答案

上下文被称为最后一次当你调用关闭,你必须处理的对象处理异常,可能抛出

Context gets called one last time when you call Close, you must handle the object disposed exception that could get thrown

static void Context(IAsyncResult result)
{
    HttpListener listener = (HttpListener)result.AsyncState;

   try
   {
        //If we are not listening this line throws a ObjectDisposedException.
        HttpListenerContext context = listener.EndGetContext(result);

        context.Response.Close();
        listener.BeginGetContext(Context, listener);
   }
   catch (ObjectDisposedException)
   {
       //Intentionally not doing anything with the exception.
   }
}
阅读全文

相关推荐

最新文章