干净地中断HttpListener的BeginGetContext方法 [英] Cleanly interrupt HttpListener's BeginGetContext method

查看:1038
本文介绍了干净地中断HttpListener的BeginGetContext方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.dll中出现了类型为'System.ObjectDisposedException'的第一次机会异常。
程序'[2568]沙箱Console.vshost.exe :托管(v4.0.30319)已退出,代码为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?

推荐答案

上次调用Close时调用了上下文,必须处理可能抛出的对象处理异常

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

这篇关于干净地中断HttpListener的BeginGetContext方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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