'localhost port is changing in Visual Studio 2017

We have an API application made in ASP.NET Core, with Visual Studio 2017. We have 4 developers working in this project and sometimes the port of the project changes without any of us do this alteration.

Here is the debug configuration of the application: enter image description here

Has this ever happened to anyone?



Solution 1:[1]

The port may be changing because launchSettings.json is ignored by source control. This common gitignore file, for example, excludes:

**/Properties/launchSettings.json

Visual Studio 2017 stores ASP.NET Core server settings (for both IIS Express and Kestrel) in this file. If it's ignored by source control, it will be regenerated on each machine with a random port. If you check the file in, every machine will use the same server settings.

Solution 2:[2]

Change the port binding in

.vs/config/applicationhost.config

on the node

configuration/system.applicationHost/sites/site[<name>]/bindings/binding[bindingInformation]

from an value like

<binding protocol="http" bindingInformation="*:5000:*" />

to an value like this

<binding protocol="http" bindingInformation=":5000:" />

Solution 3:[3]

You can use UseUrls for that:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseUrls("http://localhost:5000/")
            .Build();

        host.Run();
    }
}

UPD:

An alternative by passing arguments:

dotnet run --urls http://0.0.0.0:5000

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nate Barbettini
Solution 2 Manuel
Solution 3 Taras Kovalenko