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

I tried [these answers](https://stackoverflow.com/questions/37365277/how-to-specify-the-port-an-asp-net-core-application-is-hosted-on) 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));

```

Answer: 
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](https://i.stack.imgur.com/iEHfO.png)](https://i.stack.imgur.com/iEHfO.png)

[![enter image description here](https://i.stack.imgur.com/hK8jO.png)](https://i.stack.imgur.com/hK8jO.png)
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