gpt4 book ai didi

c# - WebHostBuilder。如何在 .NET-Core 2.0 中使用 CommandLine 设置 url 地址?

转载 作者:行者123 更新时间:2023-11-30 12:08:54 24 4
gpt4 key购买 nike

我使用下面的代码在我的 .Net-Core 1.2 环境中添加 CommandLine,我可以使用像 dotnet run --urls "http://a.cn.com:5000"--environment 这样的命令行“生产”

public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: true)
.AddCommandLine(args)
.Build();

var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseConfiguration(config)
.Build();

host.Run();
}

迁移到 .net core 2.0 后就变成这样了

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

public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((context, logging) =>
{
logging.AddSerilog();
})
.UseStartup<Startup>()
.Build();
}

我使用相同的命令行,它失败了。.net core 2.0 如何添加命令?

供应:2017.08.17下面的 launchsetting.json :

{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5005/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Robert.Project.Web": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5006"
}
}
}

我期望的听力是正在收听http://a.cn.com:5000我得到的是:

enter image description here

并且我们不应该在 Program.cs 文件中添加任何其他代码,因为 CreateDefaultBuilder 包含 addCommandLine。更多详情,您可以查看WebHost.cs#L177

供应 2017 0817 17:44 UTC+8

[::] is the IPv6 equivalent of IPv4 0.0.0.0.

最佳答案

直接使用UseConfiguration设置args填充的url地址:

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

return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
}

And we should not add any other code in Program.cs file, cause CreateDefaultBuilder is contain addCommandLine. for more detail, you can see WebHost.cs#L177

这不完全正确,因为链接的 CreateDefaultBuilder 实现使用 ConfigureAppConfiguration 方法,而不是 UseConfiguration。并且有区别(来自相关的 Github issue ):

The ConfigureAppConfiguration was intended to configure the IConfiguration in the application services whereas UseConfiguration is intended to configure the settings of the WebHostBuilder. Since the url address is a setting on the WebHostBuilder only UseConfiguration will work here.

Note that the WebHostBuilder's configuration is added to the application's configuration but the converse is not true; configuring the application's configuration does not affect the WebHostBuilder's configuration.

关于c# - WebHostBuilder。如何在 .NET-Core 2.0 中使用 CommandLine 设置 url 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45710570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com