'Kestrel error: address already in use (dotnet core)

Summary: it works as dotnet run, but it doesn't work as dotnet myappname.dll.

My linux skills are limited, but I am trying to go by the book so I don't mix things up (following this tutorial from Scott Hanselman):

$ cd /home/myusername/dotnettest
$ dotnet run

Now listening on: http://localhost:5123

Then I move it to /var like so:

$ sudo cp -a /home/myusername/dotnettest/bin/Debug/netcoreapp1.1/publish /var/dotnettest

Finally I test if it works there as well:

$ dotnet dotnettest.dll

Then it fails:

info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]
      User profile is available. Using '/home/myusername/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.

Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -98 EADDRINUSE address already in use) ---> Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.UvException: Error -98 EADDRINUSE address already in use
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.Libuv.Check(Int32 statusCode)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.UvTcpHandle.GetSockIPEndPoint()
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.TcpListener.CreateListenSocket()
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Listener.<>c.<StartAsync>b__6_0(Object state)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.KestrelEngine.CreateServer(ServerAddress address)
   at Microsoft.AspNetCore.Server.Kestrel.KestrelServer.Start[TContext](IHttpApplication`1 application)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host, CancellationToken token, String shutdownMessage)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
   at WebApplication.Program.Main(String[] args) in /home/myusername/dotnettest/Program.cs:line 27
Aborted (core dumped)

I've been careful in trying to stop nginx.

I've checked if anything is listening to :5123 with the command:

$ lsof -i tcp:5123

And nothing seems to come up.



Solution 1:[1]

The following command help to find the 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:[2]

VS2019 - In my case, I was able to work around this by changing the port Kestrel was using. Here are two options that worked for me (pick one):

  1. Add an "applicationUrl" setting to the launchSettings.json file (found under the Properties folder in VS2019). Using "https://localhost:5201" in the example below:

    "profiles": {
      "IIS Express": {
          ...
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
        "Host": {
          ...
        }
      },
      "Your.App.Namespace.Api": {
        ...
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "applicationUrl": "https://localhost:5201;http://localhost:5200"
      }
    }
    
  2. Add a "UseUrls" fluent method invocation to the WebHostBuilder in Program.Main(). i.e. "http://localhost:5200/" in the example below:

    using (var host = new WebHostBuilder()
    .UseKestrel(o => o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30))
    ...
    .UseStartup<Startup>()
    .UseUrls("http://localhost:5200/")
    

Solution 3:[3]

in linux/mac

ps aux | grep "dotnet"

Find the process and then run

kill -9 <process_id>

Solution 4:[4]

It turns out it was not correctly rebuilt after changing the Program.cs configuration to listen to :5123. And the published version was using :5000 instead of :5123.

At the same time, the port :5000 was being used by a different dotnet process (which I found through sudo netstat -ltp and killed afterwards). That's why the error was "address already in use". After killing the process, dotnet dotnettest.dll ran OK but at port :5000 (not :5123 yet).

I then made sure the project was correctly rebuilt, I deleted the /publish folder just in case, then dotnet publish. Important note: I had to copy hosting.json manually to the built folder (also to the publish folder afterwards). Now it's listening to :5123.


Steps:

  • Make sure you don't have any other process listening to the default port (:5000), using sudo netstat -ltp.
  • Make sure the project is properly rebuilt, and includes the new configuration listening to :5123 (for this you have to include hosting.json in your project, so it is copied at build and publish).

Solution 5:[5]

In my case, using ASP.NET CORE 3.1 my app was kind enough to write the port number and I confirmed that it is not used. The exception was thrown because I had used ListenAnyIP() for my heroku version of the app, but then when I deployed the app on premise behind reverse proxy, my firewall kicked in as the port was not meant to be open.

Solution 6:[6]

I know the OP is on a Mac, but this question pops up as the first Google result, so I'm adding a Windows workaround too:

Get-Process -Id (Get-NetTCPConnection -LocalPort 5001).OwningProcess

Run the above in powershell/windows terminal (replace 5001 with your port number). Then kill the process.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Paul Schroeder
Solution 3 Oyeme
Solution 4
Solution 5 Bart
Solution 6 Alex from Jitbit