I'm working with C# HttpListener (.Net Core 3.1, Windows)
According to the following documentation, GetContext method is expected to throw an ObjectDisposedException if the listener is closed while waiting. For some reason, the method call remains blocked even after the listener is closed.
Code Sample to illustrate the issue:
private static void Listen(HttpListener listener)
{
var prefixes = new List<String> { "http://localhost:8080/" };
prefixes.ForEach(s => listener.Prefixes.Add(s));
listener.Start();
Console.WriteLine("Listen: before GetContext");
try
{
listener.GetContext();
}
catch (Exception)
{
Console.WriteLine("Listen: exception was thrown");
}
Console.WriteLine("Listen: end of function");
}
private static void Main(string[] args)
{
var listener = new HttpListener();
var listeningTask = Task.Run(() => Listen(listener));
Task.Delay(1000).Wait();
Console.WriteLine("Main: before stop");
listener.Close();
Console.WriteLine("Main: before listeningTask.Wait");
listeningTask.Wait();
Console.WriteLine("Main: done!");
}
expected output:
Listen: before GetContext
Main: before stop
Main: before listeningTask.Wait
Listen: exception was thrown
Listen: end of function
Main: Done!
actual output:
Listen: before GetContext
Main: before stop
Main: before listeningTask.Wait