繁体   English   中英

环境变量已设置,但 HostingEnvironment.EnvironmentName 未更新

[英]Environment variable set but HostingEnvironment.EnvironmentName not updated

我正在运行 .net 核心 2.2 并在 windows 服务中托管 asp.net 核心。
例如。 看到这个https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.2&tabs=visual-studio

我将环境变量 ASPNETCORE_ENVIRONMENT 设置为“Dev”

只是为了确认,在我的launchsettings.json

  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT ": "Dev"
  }

在此处输入图像描述

启动时 HostingEnvironment.EnvironmentName 的值没有更新,并且仍然具有默认的“Production”。 为什么不是“开发”?

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)                
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddEventLog();
        })
        .ConfigureAppConfiguration((context, config) =>
        {

            // Configure the app here.
            var env = context.HostingEnvironment;
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                              .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

        })
        .UseStartup<Startup>();

}

我花了很长时间试图找出在这种情况下对我来说最好的事情。

我最终从 Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder 中获取了代码,并在构建器上添加了 .UseEnvironment 扩展。

            builder.UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development");

我默认使用 Development 而不是通常首选的 Production,但这是个人喜好。

它是有限的,如果你想聪明一点并检查命令行 arguments 等等,请随意。 这是一个开始的地方。

public static IHostBuilder CreateDefaultBuilder(string[] args)
{
    var builder = new HostBuilder();

    // This line has been added to read the environment variable.
    builder.UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development");

    builder.UseContentRoot(Directory.GetCurrentDirectory());
    builder.ConfigureHostConfiguration(config =>
    {
        config.AddEnvironmentVariables(prefix: "DOTNET_");
        if (args != null)
        {
            config.AddCommandLine(args);
        }
    });

    builder.ConfigureAppConfiguration((hostingContext, config) =>
    {
        var env = hostingContext.HostingEnvironment;

        config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

        if (env.IsDevelopment() && !string.IsNullOrEmpty(env.ApplicationName))
        {
            var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
            if (appAssembly != null)
            {
                config.AddUserSecrets(appAssembly, optional: true);
            }
        }

        config.AddEnvironmentVariables();

        if (args != null)
        {
            config.AddCommandLine(args);
        }
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
        var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

        // IMPORTANT: This needs to be added *before* configuration is loaded, this lets
        // the defaults be overridden by the configuration.
        if (isWindows)
        {
            // Default the EventLogLoggerProvider to warning or above
            logging.AddFilter<EventLogLoggerProvider>(level => level >= Microsoft.Extensions.Logging.LogLevel.Warning);
        }

        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        logging.AddConsole();
        logging.AddDebug();
        logging.AddEventSourceLogger();

        if (isWindows)
        {
            // Add the EventLogLoggerProvider on windows machines
            logging.AddEventLog();
        }
    })
    .UseDefaultServiceProvider((context, options) =>
    {
        var isDevelopment = context.HostingEnvironment.IsDevelopment();
        options.ValidateScopes = isDevelopment;
        options.ValidateOnBuild = isDevelopment;
    });

    return builder;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM