【问题标题】:Cannot use `--urls "http:*:8080"` to overwrite the port?不能使用 `--urls "http:*:8080"` 覆盖端口?
【发布时间】:2018-02-25 06:30:47
【问题描述】:

以下链接表明该端口可以被命令行参数dotnet run --urls "http://*:8080" 覆盖。

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x#overriding-configuration

以下是我的 Asp.Net 核心应用程序的 Program 类。但是,它仍然显示它正在收听http://localhost:5000

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

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var env = hostingContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
                config.AddEnvironmentVariables();
                config.AddCommandLine(args);
            })
            .ConfigureLogging((hostingContext, logging) => 
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddConsole();
                logging.AddDebug();
            })
            .UseStartup<Startup>()
            .Build();
}

【问题讨论】:

    标签: asp.net asp.net-core .net-core


    【解决方案1】:

    为了影响WebHost 设置,您需要使用IConfiguration 的实现调用UseConfiguration。在您的示例中,您仅使用ConfigureAppConfiguration,这不适用于WebHost

    这是一个例子:

    var webHostConfiguration = new ConfigurationBuilder()
        .AddEnvironmentVariables()
        .AddCommandLine(args)
        .Build();
    
    return WebHost.CreateDefaultuBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .UseConfiguration(webHostConfiguration)
        .ConfigureAppConfiguration(...) // No changes.
        .Build();
    

    【讨论】:

      【解决方案2】:

      使用 WebHost.CreateDefaultBuilder(args) 仅适用于 ASP.NET Core 2.1 版本的 url 覆盖。

      有一个/MetaPackages/issues/221关于这个,fix主要在CreateDefaultBuilder方法中添加了以下逻辑

      if (args != null)
      {
         builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
      }
      

      所以现在你需要直接添加这段代码:

      WebHost.CreateDefaultBuilder(args)
              .UseContentRoot(Directory.GetCurrentDirectory())
              .UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build())
              ...
      

      以防万一您有兴趣,为什么以下内容不能解决问题

         .ConfigureAppConfiguration((hostingContext, config) =>
         {
            ...
            config.AddCommandLine(args);
         })  
      

      检查这个SO post

      【讨论】:

        【解决方案3】:

        文档说“。命令行参数覆盖了 hosts.json 文件中的 urls 值”。您没有 hosting.json 文件。使用以下代码行添加hosting.json

        using Microsoft.AspNetCore;
        using Microsoft.AspNetCore.Hosting;
        using Microsoft.Extensions.Configuration;
        using System.IO;
        
        namespace NewTest
        {
            public class Program
            {
                public static void Main(string[] args)
                {
                    BuildWebHost(args).Run();
                }             
        
                public static IWebHost BuildWebHost(string[] args)
                {
                    var config = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("hosting.json", optional: true)
                        .AddCommandLine(args)
                        .Build();                                    
        
                    return WebHost.CreateDefaultBuilder(args)
                        .UseUrls("http://*:5000")
                        .UseConfiguration(config)
                        .Configure(app =>
                        {
                            //This is required otherwise application will throw the exception.                                      
                        })
                        .Build();
                }
            }
        

        }

        添加hosting.json后,配置可以被命令行覆盖。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-23
        • 1970-01-01
        • 2011-12-04
        • 2014-07-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多