Failed to bind to address (already in use) error with Visual Studio Mac API

55,925

Solution 1

Sure you can change the port every time you get that error or even restart your computer, here is the correct way to go about this

Find what is running on that port and kill the process

Terminal on mac

find the process number

lsof -i: <port number> eg lsof -i:5001

Then kill the process number

kill -9 <process number> eg - kill -9 1600

Solution 2

I know this is late answer. But it may be helpful to somebody.

There seems a bug with MAC Visual Studio, it always goes https://127.0.0.1/5001.

If you right click on your project, select options. Then Project Options Dialog will appear. On the left pane, goto Run->Configurations->Default, then on the right pane select ASP.Net Core tab. There is App URL which is being used by default. Change it to your needs.

Change IP address in App URL

Solution 3

Assuming that you are using default port:

this usually happens when a process gets corrupt and is disconnected from visual studio.

If you are on mac, open activity monitor and kill the process by name "dotnet"

If you are using a non standard port: then you need to change the port number to an available one. There is a solution from Amit on this thread, use that to change the port.

Solution 4

The port 5001 has already used in your system. Change port number to 5002 or whatever you want.

You can add port number with .UseUrls into CreateWebHostBuilder

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

Or if you use visual studio code, just replace args section in .vscode -> launch.json.

"args": ["urls=http://localhost:5002"]

Solution 5

For those who come here because they updated to MacOs Monterey. It has services listening by default on ports 5000 and 7000.

If you are blocked on those ports, running lsof -i tcp:5000 (or 7000) will show something like:

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)

For now, you can manually stop the OS from listening on these ports by unchecking AirPlay Receiver in the Apple menu -> System Preferences -> Sharing section.

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

Share:
55,925

Related videos on Youtube

ChickensDontClap
Author by

ChickensDontClap

Updated on July 09, 2022

Comments

  • ChickensDontClap
    ChickensDontClap almost 2 years

    I'm attempting to create a Web API via .Net Core. I'm just using the boilerplate ValuesController as a Hello World. When I run the project, I get the following error:

    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
    

    I'm assuming this is just a simple setting problem, but I haven't been able to find anything while Googling. Is there anything that I need to set to allow me to debug the project on Mac OS?

    • Ash
      Ash about 5 years
      While this may not be a solution to the underlying problem, a quick fix to get it working again is to execute kill -9 $(lsof -i:PORT -t) 2> /dev/null, where PORT is the port that is claimed to be already in use (e.g., 5001)
  • Nairi Areg Hatspanyan
    Nairi Areg Hatspanyan about 4 years
    I changed it but still it doesn't work when I change it and then run it it again gives an error and when I check the url is again localhost:5001http://localhost:5000
  • RuntimeError
    RuntimeError over 3 years
    @Amit, you saved my day. Works for me! Thanks a lot :)
  • umutesen
    umutesen over 3 years
    you would force quit 'dotnet' in Activity Monitor app for Mac
  • ScubaSteve
    ScubaSteve over 3 years
    I've changed mine to be different ports in the launchSettings.json and in webBuilder.UseUrls and it STILL uses different ports than what I put in there.
  • Lennert
    Lennert over 2 years
    Thanks, this answer finally helped me!
  • Prashant Abdare
    Prashant Abdare over 2 years
    I know the answer is specific to MAC but also works fine on Linux as well. (I know MAC is Linux only but just put it here if someone doesn't know that :) )
  • Reza Taba
    Reza Taba over 2 years
    On UBUNTU: sudo kill -9 'sudo lsof -t -i:5001'
  • MSicc
    MSicc over 2 years
    most simple answer and solution. I had two stale dotnet processes when this happened to me. should have far more upvotes.