30

据我了解,为 ASP Dotnet Core 2 preview 1/2 设置监听端口的正确方法是在 appsettings.json 中创建一个 Kestrel 部分,格式如下:

"Kestrel": {
    "EndPoints": { //Could also be Endpoints, it's a bit unclear
        "Http": {
        "Address": "127.0.0.1",
    "Port": 9001 //the port you want Kestrel to run on
},

我试图在 Debian 机器上设置示例 webapp,但是当我启动应用程序时,它写出该应用程序在端口 5000 上列出,默认端口..

我知道 appsettings.json 已被读取,因为当我将日志记录级别更改为 Trace 时,我会在启动时获得更多信息,包括没有找到 Endpoints 并且应用程序将使用标准的 5000 端口。

我试图在 Github 上搜索 aspnet 源代码,我可以找到从配置中读取 Kestrel 部分的区域(https://github.com/aspnet/Identity/blob/e38759b8a2de1b7a4a1c19462e40214b43c1cf3b/samples/IdentityOIDCWebApplicationSample/MetaPackage/KestrelServerOptionsSetup .cs),但正如您所见,它看起来像一个示例项目。

我错过了什么,这不是在 ASP Dotnet core 2 中配置 Kestrel 的标准方法吗?

4

5 回答 5

63

如对已接受答案的评论中所述,2.1 支持 appsettings.json,请参阅https://blogs.msdn.microsoft.com/webdev/2018/02/02/asp-net-core-2-1-roadmap /#安全

一个工作的 appsettings.json:

"Kestrel": {
  "EndPoints": {
    "Http": {
      "Url": "http://localhost:5555"
    }
  }
}

这是一个 Program.cs 使用(由“dotnet new webapi”创建):

WebHost.CreateDefaultBuilder(args)

GitHub 中的相关源代码 https://github.com/aspnet/MetaPackages/blob/master/src/Microsoft.AspNetCore/WebHost.cs#L163

options.Configure(builderContext.Configuration.GetSection("Kestrel"));

https://github.com/aspnet/MetaPackages/blob/master/src/Microsoft.AspNetCore/WebHost.cs#L169

config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
于 2018-08-29T12:41:57.623 回答
11

通过 appsettings.json 对 Kestrel 配置的支持已在 2.0 中删除。

请参阅问题评论:

kestrel 配置文件支持已从 2.0.0 中删除。配置值需要在初始化代码中手动读取。

为了解决这个问题,您可以在 program.cs 中执行以下操作:

public static IWebHost BuildWebHost(string[] args) =>
 WebHost.CreateDefaultBuilder(args)
 .UseStartup < Startup > ()
 .UseKestrel((hostingContext, options) => 
 { 
  if (hostingContext.HostingEnvironment.IsDevelopment) {
   options.Listen(IPAddress.Loopback, 9001);
   options.Listen(IPAddress.Loopback, 9002, listenOptions => {
    listenOptions.UseHttps("certificate.pfx", "password");
   });
  }
 })
 .Build();
于 2017-10-12T01:42:38.103 回答
3

我正在使用Program.cshosting.json文件来配置 Kestrel。例子:

    var config = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("hosting.json", optional: true, reloadOnChange: true)
                    .Build();

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

托管.json

{
    "urls": "http://localhost:4444;http://localhost:4445;"
}

以上是最新版本 dotnet core 的示例。

对于早期版本:

托管.json

{
    "server.urls": "http://localhost:4444;http://localhost:4445;"
}
于 2017-07-28T18:11:22.950 回答
1

要使用 Kestrel 运行 Visual Studio,只需编辑appsettings.json并添加如下配置(使用 NetCore 2.0 和 2.1 测试):

"profiles" : {
    "Kestrel": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:6969/"
    }
}
于 2018-04-03T19:37:56.260 回答
0

我遇到了同样的问题,即我在 appsettings.json 中的 Kestrel 配置没有被拾取。从这篇关于从 asp.net core 2.0 迁移到 2.1 的文章中,我将引导代码更新为如下所示,它对我有用。

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

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    return WebHost.CreateDefaultBuilder(args) 
        .UseStartup<Startup>();
}
于 2018-08-14T07:48:06.037 回答