c# .net .net-4.0 httplistener

c# - Interupt limpiamente el método BeginGetContext de HttpListener



.net .net-4.0 (2)

El contexto se llama por última vez cuando se llama a Cerrar, debe manejar la excepción de objeto dispuesto que podría lanzarse

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

Estoy usando un HttpListener y estoy usando BeginGetContext para obtener mi objeto de contexto. Quiero cerrar mi HttpListener limpiamente, pero si intento hacer un cierre en el oyente recibo una excepción y esto hace que mi programa salga.

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

El programa arroja una excepción en el listener.Close() sin embargo, el error nunca se muestra en Visual Studio, la única nota que obtengo es la siguiente en la pantalla de salida de depuración:

Se produjo una excepción de primera oportunidad del tipo ''System.ObjectDisposedException'' en System.dll
El programa ''[2568] Sandbox Console.vshost.exe: Managed (v4.0.30319)'' ha salido con el código 0 (0x0).

Pude obtener la verdadera exención de 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*)

¿Qué debo hacer para poder cerrar mi HttpListener limpiamente?


Puedes agregar esta línea

if (!listener.IsListening) { return; } HttpListenerContext context = listener.EndGetContext(ctx);