Reputation: 41
The following DotNet Core 2 project that uses HTTPListener works when run on Windows but throws a HttpListenerException when run on Ubuntu.
static void Main(string[] args)
{
Console.WriteLine("Started.");
HttpListener listener = new HttpListener();
listener.Prefixes.Add(@"http://+:83/");
listener.Start();
ThreadPool.QueueUserWorkItem((c) =>
{
Console.WriteLine("Webserver processing...");
}, listener.GetContext());
listener.Stop();
listener.Close();
Console.WriteLine("Stopped.");
}
On Windows, I run the process the then browse. The process exits without complaint:
Started.
Webserver processing...
Stopped.
But on Ubuntu, I run the process and then browse:
Started.
Webserver processing...
Unhandled Exception: System.Net.HttpListenerException: Address already in use
at System.Net.HttpEndPointManager.GetEPListener(String host, Int32 port, HttpListener listener, Boolean secure)
at System.Net.HttpEndPointManager.RemovePrefixInternal(String prefix, HttpListener listener)
at System.Net.HttpEndPointManager.RemoveListener(HttpListener listener)
at System.Net.HttpListener.Close(Boolean force)
at System.Net.HttpListener.Dispose()
at System.Net.HttpListener.Close()
at ListenTest.Program.Main(String[] args) in Program.cs:line 30
Aborted
So it throws on listener.Close().
I could catch the HttpListenerException and ignore it, but the next time I run the process it throws the same error and message on listener.Start(). As it doesn't release the socket from the first time I run it, I need to wait a minute or two before the OS release the socket/port for reuse.
If I comment out the ThreadPool.QueueUserWorkItem() call, to disallow browsing to the port then the program exits nicely without throwing.
Any ideas on getting this to work on Ubuntu would be greatly appreciated! :)
Update: This is due to be fixed in .NetCore 2.1.0 (according to https://github.com/dotnet/corefx/issues/25016)
Upvotes: 3
Views: 2425
Reputation: 41
This is due to be fixed in .NetCore 2.1.0 (according to https://github.com/dotnet/corefx/issues/25016)
Upvotes: 1