DotNet Core 2:HttpListener在Ubuntu上不起作用

安德鲁

以下使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

DisplayAttribute在Dotnet Core好友类中不起作用

Jwt代码在.NET Core 2上不起作用

DotNet Core 2 Seed用户和角色

Dotnet Core Web API返回空列表 延迟加载不起作用

SHQueryRecycleBin在.net Core上不起作用

AddOAuthlinkedin dotnet core 2.0

Dotnet Core / SignalR通信

使用节点服务从dotnet Core 2创建PDF文档

DotNet Core 2控制台应用程序配置

Visual Studio Mac与dotnet core 2不兼容

UseStatusCodePagesWithReExecute()在.NET Core 3.1上不起作用

Dotnet Core 2.2 libgdipplus异常

netstandard 和 dotnet core 的分离

Dotnet Core Nuget设置代理

dotnet core 2.0 ProjectReference 错误

data-ajax-update和data-ajax-mode =“ replace”在dotnet core jquery不引人注目的ajax中不起作用

Dotnet Core 2泊坞窗:找不到与命令“ dotnet-watch”匹配的可执行文件

在查询参数中使用%2B的ASP.net Core 3.0 GET方法在Azure上不起作用

Aurelia .NET Core-浏览器同步在.NET Core上不起作用

使用Ubuntu上的dotnet core从目录路径获取驱动器信息

在EC2 Linux AMI 2(C6G)上安装dotnet core

使用appsettings.json配置Kestrel监听端口Dotnet Core 2预览2

dotnet core 2 API和守护程序应用程序中的Azure AD身份验证

dotnet core 2由于恢复时间长,构建时间长

在dotnet core 2中的不同环境中运行不同的数据库类型

如何使用dotnet core以编程方式获取ec2实例状态

在dotnet core 2中生成Razor HTML电子邮件

Web项目中的参考类库.Net Standard 2.0时Dotnet core2 Intellisense损坏

在Mac上使用VS for Mac(DotNet Core 2)通过Migration CLI更新数据库