【问题标题】:How can I configure endpoints in Kestrel?如何在 Kestrel 中配置端点?
【发布时间】:2017-10-22 20:51:37
【问题描述】:

我正在学习如何在 ASP.NET Core 2 中使用,但遇到了一个相当烦人的问题。每当我运行我的应用程序时,Kestrel 服务器都会忽略我的端点配置,而是开始侦听随机端口。不用说,这不是我所期望的行为。在挖掘服务器日志时,我也看到了这条消息:

Overriding endpoints defined in UseKestrel() because PreferHostingUrls is set to true. Binding to address(es) 'http://localhost:<some random port>' instead.

到目前为止,我无法找到有关此“PreferHostingUrls”设置或如何更改它的任何文档。有谁知道我如何配置 Kestrel 以侦听指定端口而不是随机端口?

我的主机配置如下:

WebHost.CreateDefaultBuilder(args)
        .UseConfiguration(config)
        .UseKestrel(
            options =>
            {
                options.Listen(IPAddress.Loopback, 50734);
                options.Listen(IPAddress.Loopback, 44321, listenOptions =>
                {
                    listenOptions.UseHttps("pidea-dev-certificate.pfx", "****************");
                });
            })
        .UseStartup<Startup>()
        .UseIISIntegration()
        .Build();

【问题讨论】:

  • 这是2.0的新特性,在UseKestrel() 之前或之后添加.PreferHostingUrls(false)会发生什么?
  • 文档是herehere
  • @HenkHolterman 服务器仍然覆盖我配置的端点。

标签: c# asp.net port kestrel-http-server


【解决方案1】:

好的,事实证明 IISExpress 是这里的罪魁祸首。

出于某种原因,Visual Studio 2017 的默认构建配置会在 IISExpress 服务器上启动我的应用程序,该服务器不会监听我的端点配置。为了解决这个问题,我只需要切换到自定义运行配置。

总而言之,我只需要从这个切换:

到这里:

(PIdea 是我的项目名称)

【讨论】:

    【解决方案2】:

    当我使用 .NetCore 3.0 进行测试时,我在 launchSettings.json 中使用不同的端口 5000 失败了
    这是launchSettings.json文件中的设置

    "ABC": {
       "commandName": "Project",
       "launchBrowser": true,
       "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "applicationUrl": "http://localhost:5002"
     }
    

    我认为这是默认主机使用端口 5000 运行的根本原因 当有变化时,需要修改设置。

    有3种方法可以达到预期的效果:

    1. appsettings.jsonappsettings.Development.json 中添加红隼设置

      "Kestrel": {
        "EndPoints": {
          "Http": {
            "Url": "http://localhost:5002"
          },
          "Https": {
            "Url": "https://localhost:5003"
          }
        }
      },
      
    2. 使用 Kestrel 注册监听不同的端口

      public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
          WebHost.CreateDefaultBuilder(args).ConfigureKestrel(serverOptions =>
          {
              // Set properties and call methods on options
              serverOptions.Listen(IPAddress.Loopback, 5002); //Modify port
          })
         .UseKestrel()
         .UseContentRoot(Directory.GetCurrentDirectory())  
         .UseIISIntegration()
         .UseStartup<Startup>();
      

      或修改appsettings.json中的配置。

      "Host": {  
        "Port": 5002  
      } 
      

      然后像下面这样调用

      .ConfigureKestrel(serverOptions =>
          {
              // Set properties and call methods on options
              options.Listen(IPAddress.Loopback, config.GetValue<int>("Host:Port"));
          })
      

      .UseKestrel((context, serverOptions) =>
          {  
              var config = new ConfigurationBuilder()  
                 .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: false).Build();  
              options.Listen(IPAddress.Loopback, context.Configuration.GetValue<int>("Host:Port"));  
           }) 
      

      或在Startup.ConfigureServices 中,将配置的Kestrel 部分加载到Kestrel 的配置中:

       public void ConfigureServices(IServiceCollection services)
       {
            services.Configure<KestrelServerOptions>(
                Configuration.GetSection("Kestrel"));
       }
      
    3. 使用 UseUrls

       public class Program
       {      
           public static void Main(string[] args)
           {
               CreateHostBuilder(args).Build().Run();
           }
      
           public static IHostBuilder CreateHostBuilder(string[] args) =>
               Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(builder =>
               {
                   builder.ConfigureKestrel(serverOptions =>
                       {
                           // Set properties and call methods on options
                       })
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       .UseIISIntegration()
                       .UseUrls("http://localhost:5002")  //Add UseUrl above UseStartup
                       .UseStartup<Startup>();
               });     
            }
        }
      

       public static IWebHostBuilder CreateWebHostBuilder(string[] args)  
       {  
           var config = new ConfigurationBuilder()  
               .SetBasePath(Directory.GetCurrentDirectory())  
               .AddJsonFile("appsettings.json", optional: false)  
               .Build();  
      
           var webHost =  WebHost.CreateDefaultBuilder(args)  
               .UseUrls($"http://localhost:{config.GetValue<int>("Host:Port")}")  
               .UseKestrel()  
               .UseStartup<Startup>();  
      
           return webHost;  
       }  
      

    【讨论】:

      【解决方案3】:

      添加

      "Kestrel": {
        "EndPoints": {
          "Http": {
            "Url": "http://localhost:5002"
          },
          "Https": {
            "Url": "https://localhost:5003"
          }
        }
      }  
      

      appsettings.json

      【讨论】:

        猜你喜欢
        • 2017-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多