gpt4 book ai didi

ubuntu - 点网核心 2 : HttpListener not working on Ubuntu

转载 作者:太空宇宙 更新时间:2023-11-03 16:57:47 25 4
gpt4 key购买 nike

以下使用 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 )

关于ubuntu - 点网核心 2 : HttpListener not working on Ubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46972797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com