【问题标题】:Specifying Kestrel port with command line arg for .NET Core 3.1使用 .NET Core 3.1 的命令行 arg 指定 Kestrel 端口
【发布时间】:2020-05-22 23:04:12
【问题描述】:

这是我的 Program.CS

public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .UseSerilog()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureKestrel(serverOptions =>
                {
                    // Set properties and call methods on options
                })
                .UseKestrel()
                .UseIISIntegration()
                .UseStartup<Startup>();
            })
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true);
                config.AddCommandLine(args);
            });
    }

我正在通过以下命令运行应用程序:

dotnet run --server.urls http://localhost:59708

但是,该应用程序只会侦听端口 59707,而不是端口 59708。我正在尝试使用命令行参数设置端口,以便可以在单独的静态端口上运行应用程序的多个实例

【问题讨论】:

标签: c# .net .net-core kestrel-http-server kestrel


【解决方案1】:

像这样拨打.UseKestrel()

.UseKestrel((ctx, opt) =>
{
    var ipAddress = IPAddress.Parse("127.0.0.1")
    var port = 80;
    opt.Listen(ipAddress, port);
    // for HTTPS
    //opt.Listen(ipAddress, httpsPort, listenOptions =>
    //{
    //    listenOptions.UseHttps("certificate.pfx", "password");
    //});
}

您可以轻松找出如何解析命令行参数。选项几乎是无穷无尽的......

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2020-05-12
    • 2020-07-14
    • 2020-07-14
    相关资源
    最近更新 更多