7

I am writing my first ASP.NET Core 2.0 web REST API application by following this tutorial. However my specific question is about the code you get in the Program.cs file when you create a standard ASP.NET Core Web Application in VS2017, it is the same code described here:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace WebApplication5
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
}

I have my app working fine when I debug in VS2017, so next step that I did was to make it a standalone app according to this tutorial, which works fine and gives me an executable I can run (I'm on windows 10 x64).

The problem now is that this executable starts the webserver on port 5000, but I'd like to be able to configure the listening urls via a command line parameter.

By looking at the code above, we can see that args is passed to WebHost.CreateDefaultBuilder(args), so I'm assuming any command line arguments are interpreted by this function, however I cannot figure out what I have to pass on the command line in order to get the server to listen on another port.

I have tried the following options:
- MyApp.exe --UseUrls="http://*:5001"
- MyApp.exe --UseUrls=http://*:5001
- MyApp.exe --server.urls=http://*:5001
- MyApp.exe urls="http://*:5001"

And various other combinations like that... The app starts but keeps listening only on port 5000.

I'm beginning to think I'm trying something which is not possible :) So is it really not possible or am I missing something?

5
  • Where are you configuring/building the IConfiguration ??
    – Tseng
    Commented Aug 21, 2017 at 18:48
  • It's a bit dated, but Can't set the port for Net.Core application in launchSettings.JSON, back then it was dotnet run server.urls=http://0.0.0.0:4000 (or hence: MyApp.exe server.urls=http://0.0.0.0:4000. Not sure it works via the constructor, but you can do it with the configuration builder too
    – Tseng
    Commented Aug 21, 2017 at 18:53
  • I think the IConfiguration is built inside CreateDefaultBuilder() function
    – Alex
    Commented Aug 21, 2017 at 18:55
  • Dunno w/o watching in the source on GitHub, but the below works well in my projects .We can now naturally build the IConfiguration within program.cs
    – Tseng
    Commented Aug 21, 2017 at 18:57
  • Are you still interested in what's going on with this? I just saw the question pop up and went deep into the source to understand why the CLAs are not being read by default. I have an explanation, but I don't want to waste time writing it up if you're no longer interested. Commented Dec 13, 2017 at 19:37

1 Answer 1

11

In linux I am using: ./MYAPP urls=http://*:8081 &, but you need to modify your code for that. Try changing your code accordingly:

    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var configuration = new ConfigurationBuilder().AddCommandLine(args).Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(configuration)
            .UseStartup<Startup>()
            .Build();
    }
2
  • When run in Docker you could also consider adding in EnvironmentVariables after commandline arguments (or before)
    – André
    Commented Jun 20, 2018 at 17:47
  • If I am correct, this isn't necessary anymore in .net core 2.2. (but YMMV)
    – André
    Commented Feb 26, 2019 at 19:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.