无法绑定到地址(已在使用中)错误与 Visual Studio Mac API

Chi*_*lap 14 macos visual-studio .net-core

我正在尝试通过 .Net Core 创建一个 Web API。我只是使用样板 ValuesController 作为 Hello World。当我运行项目时,出现以下错误:

System.IO.IOException: "Failed to bind to address https://127.0.0.1:5001: address already in use." ---> System.Exception {Microsoft.AspNetCore.Connections.AddressInUseException}: "Address already in use" ---> System.Exception {System.Net.Sockets.SocketException}: "Address already in use"
  at at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)\n   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)\n   at System.Net.Sockets.Socket.Bind(EndPoint localEP)\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransport.BindAsync()
  --- End of inner exception stack trace ---
  at at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransport.BindAsync()\n   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.<>c__DisplayClass22_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()\n--- End of stack trace from previous location where exception was thrown ---\n   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context)
  --- End of inner exception stack trace ---
  at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context)\n   at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context)\n   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)\n   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)\n   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)\n   at Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken cancellationToken)\n   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String shutdownMessage)\n   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token)\n   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)\n   at Sysmex.Unity.Interfaces.WebAPI.Program.Main(String[] args) in /Users/mharrison/Developer/unity-interfaces/Sysmex.Unity.Interfaces.WebAPI/Sysmex.Unity.Interfaces.WebAPI/Program.cs:17
Run Code Online (Sandbox Code Playgroud)

我假设这只是一个简单的设置问题,但在谷歌搜索时我找不到任何东西。我需要设置什么才能在 Mac OS 上调试项目吗?

小智 13

确保每次出现错误时都可以更改端口,甚至可以重新启动计算机,这是解决此问题的正确方法

查找在该端口上运行的内容并终止进程

mac上的终端

找到进程号

lsof -i: <port number> 例如 lsof -i:5001

然后kill掉进程号

kill -9 <process number> 例如 - 杀死 -9 1600

  • 我知道答案是针对 MAC 的,但在 Linux 上也能正常工作。(我知道 MAC 仅适用于 Linux,但如果有人不知道,请将其放在这里:)) (3认同)

小智 12

在 Mac 上,仅仅找到进程并杀死它是行不通的。我们需要杀死进程号。

\n
lsof -i: <port number>\n
Run Code Online (Sandbox Code Playgroud)\n

例如

\n
lsof -i:5001\n
Run Code Online (Sandbox Code Playgroud)\n

并杀死进程号

\n
kill -9 <process number / PID>\n
Run Code Online (Sandbox Code Playgroud)\n

例如

\n
kill -9 438\n
Run Code Online (Sandbox Code Playgroud)\n

原因是使用该端口的进程名为ControlCenter,它是macOS本机应用程序,杀死该进程后会立即重新启动。

\n

最终对我有用的是:

\n

在此端口上运行的进程是一个 AirPlay 服务器。您可以在以下位置停用它System Preferences \xe2\x80\xba Sharing, and unchecking AirPlay Receiver释放端口 5000

\n


Ami*_*mit 11

我知道这是迟到的答案。但它可能对某人有帮助。

MAC Visual Studio 似乎有一个错误,它总是转到https://127.0.0.1/5001

如果您右键单击您的项目,选择选项。然后将出现项目选项对话框。在左侧窗格中,转到 Run->Configurations->Default,然后在右侧窗格中选择 ASP.Net Core 选项卡。有默认使用的应用程序 URL。将其更改为您的需要。

更改应用程序 URL 中的 IP 地址


Ton*_*ong 9

端口 5001 已在您的系统中使用。

Windows,使用任务管理器终止进程“dotnet core host”

Mac,在“活动监视器”应用程序中强制退出“dotnet”

在此输入图像描述

  • 您将在 Mac 的“活动监视器”应用程序中强制退出“dotnet” (2认同)

小智 9

对于那些因为更新到 MacOs Monterey 而来到这里的人。默认情况下,它有在端口 5000 和 7000 上侦听的服务。

如果您在这些端口上被阻止,运行lsof -i tcp:5000(或 7000)将显示类似以下内容:

COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ControlCe 371 me   17u  IPv4 0xad81aeb96e1d0669      0t0  TCP *:commplex-main (LISTEN)
ControlCe 371 me   18u  IPv6 0xad81aeb9709a5f91      0t0  TCP *:commplex-main (LISTEN)
Run Code Online (Sandbox Code Playgroud)

目前,您可以通过在 Apple 菜单 -> 系统偏好设置 -> 共享部分中取消选中 AirPlay 接收器来手动停止操作系统侦听这些端口。

https://developer.apple.com/forums/thread/682332

  • 谢谢,这个答案终于帮助了我! (2认同)

Kal*_*pat 8

假设您使用的是默认端口:

这通常发生在进程损坏并与 Visual Studio 断开连接时。

如果您使用的是 mac,请打开活动监视器并按名称“dotnet”终止进程

如果您使用的是非标准端口:那么您需要将端口号更改为可用的端口号。Amit 在这个线程上有一个解决方案,使用它来更改端口。


Cel*_*rim 7

端口 5001 已在您的系统中使用。将端口号更改为 5002 或任何您想要的。

您可以使用.UseUrls添加端口号到CreateWebHostBuilder

  public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://localhost:5002")
            .UseStartup<Startup>();
Run Code Online (Sandbox Code Playgroud)

或者,如果您使用 Visual Studio 代码,只需替换.vscode -> launch.json 中的args部分。

"args": ["urls=http://localhost:5002"]
Run Code Online (Sandbox Code Playgroud)


Irs*_*shu 6

如果您使用的是 Mac,您可以找到在端口上运行的进程lsof -i tcp:<PORT>

要终止端口上运行的进程,您可以尝试npx kill-port <PORT>