12

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.

Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
  • After a `sudo netstat -ltp` I've seen that there was a `dotnet` process permanently listening to :5000 (I left something running? I don't think there's a default background process doing that). So then I retry `dotnet dotnettest.dll` and it works... but it listens to :5000 (instead of :5123). I'll have to check what I did wrong, but looks like it's taking into account the changes I made in `Program.cs` (it's weird because `dotnet run` was listening to :5123 as expected). – Xavier Peña Jan 14 '17 at 19:16

6 Answers6

23

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

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
3

in linux/mac

ps aux | grep "dotnet"

Find the process and then run

kill -9 <process_id>
Oyeme
  • 11,088
  • 4
  • 42
  • 65
3

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 by running:

kill theProcessNumberHere
dalcam
  • 1,027
  • 11
  • 28
Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
  • Thanks man - but would have been even more helpful if you included the line to kill the process - even though its simply kill #:D - i forgot it was that easy and had to google it! – dalcam Jul 13 '22 at 02:38
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/")
    
Paul Schroeder
  • 1,460
  • 1
  • 14
  • 21
1

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).
Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
0

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.

Bart
  • 2,167
  • 2
  • 15
  • 21