1

All attempts to expose a .NET 6 web API on port 8888 fail.

I tried these answers and various other articles online, by adding urls in appsettings.json and configuring the port in Program.cs without success.

Update: The web API starts on the default port and not on the port I want to expose it to. When configuring ports in Program.cs, I receive

System.InvalidOperationException: 'Changing the URL is not supported because Addresses IsReadOnly.'

app.Run("http://localhost:8888"); // This code results in Exception above

or

System.NotSupportedException: 'Collection was of a fixed size.'

app.Urls.Add("http://localhost:8888"); // The Exception above occurs after running this line of code

Running the code below doesn't change the port, and the API starts on the default port

builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(8888));
Tassisto
  • 9,877
  • 28
  • 100
  • 157
  • HI. Please add an error message to your answer – OlegI Jan 20 '22 at 11:08
  • Also, how do you *host* the deployed application? On IIS, the port is configured by IIS itself. You could modify it in the virtual directory's `web.config` file – Panagiotis Kanavos Jan 20 '22 at 11:10
  • From cmd.exe use >Netstat -a and check to see if port is already used by another application. – jdweng Jan 20 '22 at 11:13
  • @PanagiotisKanavos Can you show me how to configure ports in Program.cs? I tried to do it without any success. – Tassisto Jan 20 '22 at 11:17
  • What did you try? Have you checked the [Working with ports](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0#working-with-ports) section in the Minimal API docs? Have you tried `app.Run("http://localhost:8888");` ? Or `app.Urls.Add("http://localhost:8888");`? Or one of the other options? – Panagiotis Kanavos Jan 20 '22 at 11:17
  • Are you using IIS or Kestrel for hosting? `appsettings.json` is only used by Kestrel. – Panagiotis Kanavos Jan 20 '22 at 11:19
  • I receive System.InvalidOperationException: 'Changing the URL is not supported because Addresses IsReadOnly.' when applying app.Run("http://localhost:8888"); – Tassisto Jan 20 '22 at 11:24
  • I get System.NotSupportedException: 'Collection was of a fixed size.' when applying app.Urls.Add("http://localhost:8888"); – Tassisto Jan 20 '22 at 11:25

3 Answers3

5

Adding the line builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(PortNumber)); to your Program.cs file should allow you to change the default port on a .NET 6 web API.

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(8888));

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

The above change should work also when you are debugging, but another way to change this in development as mentioned in the other answer, you can use the launchSettings.json file under the properties folder of the solution, documentation related to this file and how it works can be found here, an example of what you want would be:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:31312",
      "sslPort": 44346
    }
  },
  "profiles": {
    "ProfileNameHere": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:8887;http://localhost:8888",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

The profile section will affect kestrel ports in dev environment so that is the one you should be updating to the desired value. You can also create multiple profiles with different ports and configurations.

Armando Bracho
  • 659
  • 5
  • 21
  • I already did this and no success – Tassisto Jan 20 '22 at 12:04
  • Do you receive any errors when using this? can you also provide the content of your launchSettings.json file? – Armando Bracho Jan 20 '22 at 12:13
  • No error is shown, just that the api is on a different port. – Tassisto Jan 20 '22 at 12:15
  • I used the Package Manager Console. Entered some cd commands to go to the folder where the csproj file is located. I ran the command "dotnet run" and could see the message Now listening on: localhost:8888. Using Postman, I could successfully send requests to my API which is hosted on port 8888 – Tassisto Jan 20 '22 at 12:27
  • Great to know it is working, if you want to verify that the port is working once you deploy it, then try to make a release publish profile and point it to a local folder. Run the web API .exe located in the folder and you should get your API running on the specified port. – Armando Bracho Jan 20 '22 at 12:37
  • Can I achieve this when debugging instead of running the api? The ideal goal is to have Swagger on that same port – Tassisto Jan 20 '22 at 13:40
  • I have added further information about changing the default port while debugging, this should all work along with swagger once you add it. Let me know if you have any issues with this. – Armando Bracho Jan 20 '22 at 15:14
1

There are many solutions had been noted in this page, you can check https://andrewlock.net/5-ways-to-set-the-urls-for-an-aspnetcore-app/

For me,

  • When I use an IDE, I modify the file Properties\launchSettings.json
  • When I run the app in console, I add the param --urls "http://localhost:5100"
  • When I deploy it on IIS, well, it's not a question because IIS always ask me to enter the port. There is no default port

Note: If they don't work on your side, check there any mistake. Maybe you can restart your PC to make sure the port are available.

Update:

I have tried to run at port 8888 and everything is working well (I also use .NET 6) enter image description here

enter image description here

Võ Quang Hòa
  • 2,688
  • 3
  • 27
  • 31
0

Can you try this

 public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)                 
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>).UseKestrel().UseUrls("http://0.0.0.0:8888");
                    });
        }

This works for docker environment also. Make the port as variable and get it from environment as next step

Santosh Karanam
  • 1,077
  • 11
  • 23