如何在Kestrel中配置端点?

时间:2017-10-22 20:51:38

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

我正在学习如何在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();

3 个答案:

答案 0 :(得分:3)

好的,所以原来IISExpress是罪魁祸首。

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

总结一下,我只需要改变一下:

enter image description here

到此:

enter image description here

(PIdea是我项目的名称)

答案 1 :(得分:1)

当我使用netcore 3.0进行测试时,在launchSettings.json中使用其他端口5000失败 这是文件launchSettings.json

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

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

有3种方法可以达到预期结果: 01。在appsettings.json和appsettings.Development.json中添加红

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

02。使用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>();
  
 or modify config in appsettings.json.
  
  "Host": {  
      "Port": 5002  
  } 

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

    or
       .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"));  
        }) 
    
     or 
         In Startup.ConfigureServices, load the Kestrel section of configuration into Kestrel's configuration:
         public void ConfigureServices(IServiceCollection services)
         {
              services.Configure<KestrelServerOptions>(
                Configuration.GetSection("Kestrel"));
         }

03。使用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>();
        });     
     }
 }

or 
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;  
}  

答案 2 :(得分:0)

添加

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

}

到appsettings.json。