kkkristo
kkkristo

Reputation: 68

ASP.NET app is not listening on port 5001 after deployment

I'm developing an ASP.NET Core project recently upgraded from .NET 6 to .NET 9, in VS 2022.

For the past few days for unknown (to me) reasons, I can't run the app on localhost 5001 port, after deployment (to a local folder).

When I use details below in appsettings.json, the deployed application runs on port 5001 properly:

 "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://127.0.0.1: 5000"
      },
      "Https": {
        "Url": "https://localhost: 5001"
      }}}

But then I can't launch it from VS. I didn't make any manual changes to launchsettings.json or program.cs, I use app.UseHttpsRedirection()

Is there any possibility to set default settings for development and deployment? I experienced no troubles in .NET 6, and had nothing to do with ports at all.

Upvotes: 0

Views: 114

Answers (2)

Guru Stron
Guru Stron

Reputation: 143098

There is at least one breaking change which could have affected this behaviour - see the Kestrel: Default HTTPS binding removed from .NET 7 breaking changes:

Version introduced

ASP.NET Core 7.0

Previous behavior

Previously, if no values for the address and port were specified explicitly but a local development certificate was available, Kestrel defaulted to binding to both http://localhost:5000 and https://localhost:5001.

New behavior

Users must now manually bind to HTTPS and specify the address and port explicitly, through one of the following means:

  • The launchSettings.json file
  • The ASPNETCORE_URLS environment variable
  • The --urls command-line argument
  • The urls host configuration key
  • The HostingAbstractionsWebHostBuilderExtensions.UseUrls extension method

HTTP binding is unchanged.

Without seeing the full actual setup it is a bit hard to tell what the best course of action would be, but if you add the Kestrel setup to the application.json then you should modify launchSettings.json accordingly (so it uses the same ports as configured for Kestrel). Alternatives would be moving the Kestrel setup to application.Environment_Used_For_Local_File_Deploy_Here.json (if it is not Development), or something along the options stated above (so you have correct setup).

Upvotes: 1

kkkristo
kkkristo

Reputation: 68

Ok, I managed to find an answer by myself, although I didn't find the reason of such behavior of VS, that deploys an app only with listening on port 5000. What I did - initially, following tips from Andrew Lock's article here, I added this code to program.cs:

builder.WebHost.UseUrls("http://localhost:5000", "https://localhost:5001");

It helped and set up ports properly for deploy, but also same ports for debugging. I prefered to have different ports for deployed instance and different for the one being under development. At the end I removed above code, and set a pair of ports in launchsettings.json for development, and in program.cs I added this:

if (!app.Environment.IsDevelopment())
{
    app.Urls.Add("http://localhost:8000");
    app.Urls.Add("https://localhost:8001");
}

Now it works as needed. Hope it helps anyone too.

Upvotes: 1

Related Questions