When I was trying to deploy my ASP.NET Core application on Linux operating system using dotnet WebUi.dll (WebUi is my application file name) command, I got this Unhandled exception Failed to bind to address as an address already in use.
Complete Details of the issue:
Unhandled exception. System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use.
—> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
—> System.Net.Sockets.SocketException (98): Address already in use
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
— End of inner exception stack trace —
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass30_01.<<StartAsync>g__OnBind|0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken) --- End of inner exception stack trace --- at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.DefaultAddressStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable
1 listenOptions, AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
at Microsoft.AspNetCore.Builder.WebApplication.Run(String url)
at Program.$(String[] args) in C:\Users\Srini\source\repos\WebUi\WebUi\Program.cs:line 25
Aborted (core dumped)
Problem
If we observe the issue details, it’s clearly saying it failed to bind the application to 5000 port, because its already used in some other process. When I tried to run my application with help of a command dotnet WebUi.dll (WebUi is my application file name), It is trying to run the application on port 5000 of localhost address (127.0.0.1). As my ASP.NET Core application has the settings to run the application on 5000 port. For that, we do not need to change the port number in application settings unless we are not using port 5000 anywhere else with our knowledge.
Solution
To solve this issue we need to find out which process using port 5000 and then we need to kill that particular process. To find out the process which is holding port 5000 we need to run the Linux command as lsof -i:5000 and then to kill that particular process we need to kill the process specifying its PID in Linux command kill -9 20561. In my case 20561 is the PID for the process which was holding 5000 ports and -9 is the part of the kill command. The screenshot is given below for the same.
Once the existing process which was holding port 5000 is killed, then I could run the ASP.NET Core application and it started working with the Kestrel server properly.
Summary
We could find out the root cause of the issue which is that the port was used in other processes already. For that, we could existing process which was using port 5000. This can happen if we did not stop properly our application if already run the application on port 5000. Please comment what was the problem in your case.