gpt4 book ai didi

asp.net-core - 如何在启动类中配置 Kestrel url

转载 作者:行者123 更新时间:2023-12-02 20:19:00 24 4
gpt4 key购买 nike

我正在尝试找出修改 kestrel 监听的 URL 的正确方法从 Startup 类构造函数

更新:除了下面的答案之外,我开始了解到 Startup 类并不按照我想象的方式用于配置 Kestrel。我原以为 Startup 会创建一个应用程序范围的配置对象,该对象将通过约定进行定位,但事实并非如此。正如@Tseng 指出的那样,应用程序和托管的配置是不同的问题。链接的答案和接受的答案提供了工作示例。

我在 Vagrant 中启动了一个全新的 Ubuntu 14 盒子,并根据 Microsoft 的当前说明安装了 ASP.NET Core 1.1:https://www.microsoft.com/net/core#linuxubuntu

我跑了:

  • dotnet new -t web
  • dotnet 恢复
  • dotnet 运行

这监听 http://localhost:5000默认情况下。在 Program.cs 中,我可以调用 app.UseUrls("http://*:5001) ,这可以更改 URL 和端口。

我真正想要的是通过在 Startup 类中添加新的 JSON 设置文件来按惯例更改 URL,因此我按照示例并使用此文件创建了 Hosting.JSON 文件(注意:我尝试过“server.urls”) "和 "urls"作为键)。

{
urls: "http://*:5001"
}

在 Startup.cs 中,在添加 appsettings.json 文件的默认行下方,我添加了 .AddJsonFile("hosting.json", 可选: false); (可选: false 以确保它正在拾取文件)

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("hosting.json", optional: false);
if (env.IsDevelopment())
{
// For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}

builder.AddEnvironmentVariables();
Configuration = builder.Build();
}

我已验证该设置在配置构建后存在,但在 Program.cs 中构建主机时未拾取或使用该设置

public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

host.Run();
}
}

并且应用程序仍然开始监听 localhost:5000。我(可能不正确)的理解是,通过使用正确的 key 正确命名设置,WebHostBuidler 应该选择它并使用它。

我见过其他示例,这些示例基本上摆脱了启动类并在 Program.cs 中创建配置,然后可以将其传递到对 UseConfiguration 的调用中,但我的理解是,使用启动类应该按照惯例执行此操作。

本质上我想将启动和程序分开,将hosting.JSON文件添加到带有URL的配置中,并在不需要的情况下将其拾取和使用调用UseUrls、UseConfiguration等。

我是否错过了一些明显的事情,或者试图做一些实际上不正确的事情?

根据我的评论解释为什么这不是重复的:

  • 该帖子特别指出“launcherSettings 适用于 Visual Studio F5”。我在 Linux 机器上使用命令行。与VS无关。

  • 该文章中提供的解决方案将配置构建移至 main 方法中。我特别指出,我想在 Startup 类中构建我的配置,就像默认的“dotnet new -t web”项目一样。

我不认为这是重复的,并且仍在审查 ASP.NET Core 源代码以查看这是否可行。

关于正确的 key :

https://github.com/aspnet/Hosting/blob/b6da89f54cff11474f17486cdc55c2f21f2bbd6b/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs

namespace Microsoft.AspNetCore.Hosting
{
public static class WebHostDefaults
{
public static readonly string ApplicationKey = "applicationName";
public static readonly string StartupAssemblyKey = "startupAssembly";
public static readonly string DetailedErrorsKey = "detailedErrors";
public static readonly string EnvironmentKey = "environment";
public static readonly string WebRootKey = "webroot";
public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
public static readonly string ServerUrlsKey = "urls";
public static readonly string ContentRootKey = "contentRoot";
}
}

https://github.com/aspnet/Hosting/blob/80ae7f056c08b740820ee42a7df9eae34541e49e/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs

public class WebHost : IWebHost
{
private static readonly string DeprecatedServerUrlsKey = "server.urls";

最佳答案

在您的hosting.json 文件中,您应该使用server.urls 而不是url。并且hosting.json文件需要添加到program.cs中(在main方法中),而不是在启动时添加。

这是我的 Hosting.json 文件。

{
"server.urls": "http://localhost:5010;http://localhost:5012"
}

这是Main方法。

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

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

host.Run();
}

这是屏幕截图。

Screenshot of ASP.NET Core app running

关于asp.net-core - 如何在启动类中配置 Kestrel url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41514991/

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