Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 459
ChickensDontClap Avatar asked Oct 04 '18 15:10

ChickensDontClap


4 Answers

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

like image 88
BitcoinKing Avatar answered Oct 13 '22 21:10

BitcoinKing


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

like image 20
Amit Avatar answered Oct 13 '22 21:10

Amit


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.

like image 16
Kalpesh Popat Avatar answered Oct 13 '22 21:10

Kalpesh Popat


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"]
like image 9
Celal Yildirim Avatar answered Oct 13 '22 20:10

Celal Yildirim