CodeSlinger512
CodeSlinger512

Reputation: 668

Cannot hit ASP.NET Core API from remote computer on network

I created the sample dotnet webapi project via dotnet new webapi (v3.1.101) and updated launchSettings.json's applicationUrl to first http:*:5000 then to http:0.0.0.0:5000. When I try to go to http://<MY_COMPUTER_NETWORK_NAME>:5000/WeatherForecast, it times out, yet when I locally try http://localhost:5000/WeatherForecast, it works.

I ran a React App on port 5000 and I could successfully connect to it remotely, so I don't think it is a firewall issue.

I am running on a Mac. I also tried adding .UseUrls(...) without success.

Why can't I connect remotely?

Thanks in advance!

Upvotes: 4

Views: 1037

Answers (2)

Claudio Valerio
Claudio Valerio

Reputation: 2342

dotnet run will try to automatically load MyApplication\Properties\launchSettings.json. If file launchSettings.json exists and "all addresses" binding is set:

{
  "profiles": {
    "Uno": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://0.0.0.0:5001;http://0.0.0.0:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Then running the application should give this result:

> dotnet run
info: Microsoft.Hosting.Lifetime[0]
    Now listening on: https://0.0.0.0:5001
info: Microsoft.Hosting.Lifetime[0]
    Now listening on: http://0.0.0.0:5000

and checking open ports:

> netstat -na | ? { $_ -match "5000|5001" }
  TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:5001           0.0.0.0:0              LISTENING

If launchSettings.json does not exist or exist with different name, default binding will apply:

> mv .\Properties\launchSettings.json .\Properties\launch.json
> dotnet run
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001

unless you specify the urls using specific option: server.urls="http://0.0.0.0:5000;https://0.0.0.0:5001"

Also note that the first time that any application tries to bind to 0.0.0.0:port, a window like this appears: enter image description here It is very easy to miss it if you are in multi monitor setup or if you immediately focus on other windows. Also, this dialog will set an application specific firewall rule, so maybe you created the rule for the React application and not the rule for the dotnet app.

Upvotes: 3

Ian
Ian

Reputation: 4457

I'm using VSCode in Windows and I've noticed I cannot use the dotnet run command from the terminal, I have to hit play from the Debug sidebar window for it to work, and then I can connect from other computers on the same network.

If you would like to launch with the terminal, then I think you have to manually specify your launch.json file in the options. See the --launch-profile option here: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-run

Upvotes: 1

Related Questions