gpt4 book ai didi

asp.net-core - 在构建 WebHost 之前访问托管环境

转载 作者:行者123 更新时间:2023-12-05 08:55:24 26 4
gpt4 key购买 nike

在我的 Program.cs Main 方法中,我想读取 user secrets ,配置记录器,然后构建 WebHost

public static Main(string[] args)
{

string instrumentationKey = null; // read from UserSecrets

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.ApplicationInsightsEvents(instrumentationKey)
.CreateLogger();

BuildWebHost(args).Run();
}

可以通过构建自己的配置来获取配置,但我很快就会在尝试访问托管环境属性时陷入困惑:

public static Main(string[] args)
{
var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
var configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{envName}.json", optional: true, reloadOnChange: true);

// Add user secrets if env.IsDevelopment().
// Normally this convenience IsDevelopment method would be available
// from HostingEnvironmentExtensions I'm using a private method here.
if (IsDevelopment(envName))
{
string assemblyName = "<I would like the hosting environment here too>"; // e.g env.ApplicationName
var appAssembly = Assembly.Load(new AssemblyName(assemblyName));
if (appAssembly != null)
{
configBuilder.AddUserSecrets(appAssembly, optional: true); // finally, user secrets \o/
}
}
var config = configBuilder.Build();
string instrumentationKey = config["MySecretFromUserSecretsIfDevEnv"];

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.ApplicationInsightsEvents(instrumentationKey) // that.. escallated quickly
.CreateLogger();

// webHostBuilder.UseConfiguration(config) later on..
BuildWebHost(args, config).Run();
}

在构建 WebHost 之前,是否有更简单的方法来访问 IHostingEnvironment

最佳答案

main 方法中,您无法在构建 WebHost 之前获取 IHostingEnvironment 实例,因为尚未创建托管。而且您无法正确创建新的有效实例,如 it must be initialized using WebHostOptions` .


对于应用程序名称,您可以使用 Assembly.GetEntryAssembly()?.GetName().Name


对于环境名称,请使用您当前所做的(我假设您的 IsDevelopment() 方法中使用了类似这样的内容):

var environmentName = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
bool isDevelopment = string.Equals(
"Development",
environmentName,
StringComparison.OrdinalIgnoreCase);

看,像 IHostingEnvironment.IsDevelopment() 这样的方法是 extension methods that simply do string comparison internally :

    public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}

return hostingEnvironment.IsEnvironment(EnvironmentName.Development);
}


public static bool IsEnvironment(
this IHostingEnvironment hostingEnvironment,
string environmentName)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}

return string.Equals(
hostingEnvironment.EnvironmentName,
environmentName,
StringComparison.OrdinalIgnoreCase);
}

关于 AddJsonFile 的注意事项:由于文件名在 Unix 操作系统中很敏感,有时最好使用 $"appsettings.{envName.ToLower()}.json"的。

关于asp.net-core - 在构建 WebHost 之前访问托管环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46012935/

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