以下使用HTTPListener的DotNet Core 2项目在Windows上运行时可以运行,但是在Ubuntu上运行时会抛出HttpListenerException。
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.");
}
在Windows上,我先运行进程,然后浏览。该过程退出而没有任何投诉:
Started.
Webserver processing...
Stopped.
但是在Ubuntu上,我运行该过程,然后浏览:
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
因此它引发了listener.Close()。
我可以捕获HttpListenerException并忽略它,但是下次运行该进程时,它将在listener.Start()上抛出相同的错误和消息。由于它从我第一次运行时就不会释放套接字,因此我需要等待一两分钟,然后操作系统才能释放套接字/端口以供重用。
如果我注释掉ThreadPool.QueueUserWorkItem()调用,以禁止浏览到该端口,则程序将正常退出而不会抛出异常。
关于在Ubuntu上运行它的任何想法将不胜感激!:)
更新:这将在.NetCore 2.1.0中修复(根据https://github.com/dotnet/corefx/issues/25016)
这将在.NetCore 2.1.0中修复(根据https://github.com/dotnet/corefx/issues/25016)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句