c# - 干净利落地中断 HttpListener 的 BeginGetContext 方法

标签 c# .net .net-4.0 httplistener

我正在使用 HttpListener并使用 BeginGetContext获取我的上下文对象。我想完全关闭我的 HttpListener 但如果我尝试执行 Close在监听器上我得到一个异常,它导致我的程序退出。

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 中显示,我得到的唯一注释是调试输出屏幕中的以下内容:

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 事件查看器中获得真正的执行

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?

最佳答案

当您调用 Close 时最后一次调用 Context,您必须处理可能抛出的对象处置异常

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.
   }
}

关于c# - 干净利落地中断 HttpListener 的 BeginGetContext 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351615/

相关文章:

c# - 如何复制粘贴 WPF 窗口(克隆)并且没有错误

c# - 为什么WPF中的BackgroundWorker需要Thread.Sleep来更新UI控件?

c# - 内存不足异常,即使有 30 GB 内存可用

c# - 使用 SynchronizationContext 时异步/等待死锁

c# - System.Web.dll 中发生类型 'System.InvalidOperationException' 的异常,但未在用户代码中处理

c# - 内存流作为数据库

c# - 带有很多控件的假滚动容器

.net - WPF 中的 Setter 优先级重写?

asp.net-mvc - 如何在 ASP.NET MVC5 中创建自定义脚手架模板?

c# - 条件编译和框架目标