无法在 macOS Monterey 上启动 Kestrel

Posted

技术标签:

【中文标题】无法在 macOS Monterey 上启动 Kestrel【英文标题】:Unable to start Kestrel on macOS Monterey 【发布时间】:2021-12-12 23:11:24 【问题描述】:

我将我的macOS Big Sur 升级到macOS Monterey 然后我尝试使用ASPNETCORE_URLS=http://*:5000 配置运行我的.Net Core 应用程序,它给了我如下错误

2021-10-27 19:23:25.0068|FATAL|Microsoft.AspNetCore.Server.Kestrel|Unable to start Kestrel.
Unhandled exception. System.IO.IOException: Failed to bind to address http://[::]:5000: address already in use.
 ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use

当我尝试使用以下代码查找谁在监听端口 5000

sudo lsof -nP -i:5000 | grep LISTEN

结果是;

ControlCe 2874 sametcelikbicak   17u  IPv4 0xac23fd083a725c89      0t0  TCP *:5000 (LISTEN)
ControlCe 2874 sametcelikbicak   18u  IPv6 0xac23fd082e8a6671      0t0  TCP *:5000 (LISTEN)

然后我尝试用下面的 kill 命令杀死那个监听

kill -9 2874

那个端口监听不会杀死,我再次运行sudo lsof -nP -i:5000 | grep LISTEN 来测试它,但它又给了我这个结果

ControlCe 4271 sametcelikbicak   17u  IPv4 0xac23fd0849ef2c59      0t0  TCP *:5000 (LISTEN)
ControlCe 4271 sametcelikbicak   18u  IPv6 0xac23fd083995b411      0t0  TCP *:5000 (LISTEN)

如果我尝试 ASPNETCORE_URLS=http://*:5001 运行 .Net Core 应用程序,它可以工作,我也重新启动我的 mac,但仍然报错,为什么 ASPNETCORE_URLS=http://*:5000 配置不适用于 Monterey 以及谁总是使用端口 5000

还有我的 dotnet 版本和 sdk-list 如下

➜  ~ dotnet --list-sdks
2.1.4 [/usr/local/share/dotnet/sdk]
2.1.402 [/usr/local/share/dotnet/sdk]
2.1.504 [/usr/local/share/dotnet/sdk]
3.1.201 [/usr/local/share/dotnet/sdk]
3.1.300 [/usr/local/share/dotnet/sdk]
5.0.102 [/usr/local/share/dotnet/sdk]
5.0.103 [/usr/local/share/dotnet/sdk]
5.0.300 [/usr/local/share/dotnet/sdk]
➜  ~ dotnet --list-runtimes
Microsoft.AspNetCore.All 2.1.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
➜  ~ dotnet --version
5.0.300
➜  ~

感谢您的建议。

【问题讨论】:

why ASPNETCORE_URLS=http://*:5000 config does not work with Monterey? - 因为您已经发现,端口 5000 正在使用中。 and who always use the port 5000? - 快速搜索显示它是AirPlay in the Control Center。禁用该设置或使用其他端口。 【参考方案1】:

感谢@devNull、AirPlay in the Control Center 解决了我的问题。

解决方案的基本步骤;

转到系统偏好设置>共享>取消选中AirPlay Receiver

【讨论】:

以上是关于无法在 macOS Monterey 上启动 Kestrel的主要内容,如果未能解决你的问题,请参考以下文章

MySQL brew 无法在 macOS Monterey 上运行?

Eclipse CDT 不适用于 macOS Monterey

macOS Monterey的部分新功能无法在英特尔Mac上使用

MacOS Monterey根路径下创建目录的办法

MacOS Monterey根路径下创建目录的办法

MacOS Monterey根路径下创建目录的办法