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

时间:2019-09-30 16:26:01

标签: asp.net-core .net-core

正在运行.net core 2.2,并在Windows服务中托管asp.net core。
例如。看到这个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"
  }

enter image description here

启动时,HostingEnvironment.EnvironmentName的值未更新,并且仍具有默认的“生产”。为什么不是“ Dev”?

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>();

}

1 个答案:

答案 0 :(得分:1)

在这种情况下,我花了很长时间试图找出最适合自己的事情。

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

 4:26.64 error: failed to run custom build command for `style v0.0.1 (/home/szl/src/firefox/servo/components/style)`
 4:26.64 Caused by:
 4:26.64   process didn't exit successfully: `/home/szl/src/firefox/obj-x86_64-pc-linux-gnu/release/build/style-e55deaa498a7169f/build-script-build` (exit code: 101)
 4:26.64 --- stdout
 4:26.64 cargo:rerun-if-changed=build.rs
 4:26.64 cargo:out_dir=/home/szl/src/firefox/obj-x86_64-pc-linux-gnu/x86_64-unknown-linux-gnu/release/build/style-d875f75d2d34836b/out
 4:26.64 cargo:rerun-if-changed=properties/counted_unknown_properties.py
 4:26.64 cargo:rerun-if-changed=properties/gecko.mako.rs
 4:26.64 cargo:rerun-if-changed=properties/build.py
 ......
 4:26.65 cargo:rerun-if-changed=/home/szl/src/firefox/obj-x86_64-pc-linux-gnu/layout/style/bindgen.toml
 4:26.65 cargo:rerun-if-changed=/home/szl/src/firefox/layout/style/ServoBindings.toml
 4:26.65 --- stderr
 4:26.65 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/nsGkAtoms.h:118:39: warning: offset of on non-standard-layout type 'mozilla::detail::GkAtoms' [-Winvalid-offsetof]
 4:26.65 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/js/Proxy.h:220:43: warning: offset of on non-standard-layout type 'js::BaseProxyHandler' [-Winvalid-offsetof]
 4:26.65 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/dom/BrowsingContext.h:721:29: warning: offset of on non-standard-layout type 'mozilla::dom::BrowsingContext' [-Winvalid-offsetof]
 4:26.65 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:32:1: warning: replacement function 'operator new' cannot be declared 'inline' [-Winline-new-delete], err: false
 4:26.65 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:36:1: warning: replacement function 'operator new' cannot be declared 'inline' [-Winline-new-delete], err: false
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:41:1: warning: replacement function 'operator new[]' cannot be declared 'inline' [-Winline-new-delete], err: false
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:45:1: warning: replacement function 'operator new[]' cannot be declared 'inline' [-Winline-new-delete], err: false
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:50:1: warning: replacement function 'operator delete' cannot be declared 'inline' [-Winline-new-delete], err: false
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:54:1: warning: replacement function 'operator delete' cannot be declared 'inline' [-Winline-new-delete], err: false
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:59:1: warning: replacement function 'operator delete[]' cannot be declared 'inline' [-Winline-new-delete], err: false
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:63:1: warning: replacement function 'operator delete[]' cannot be declared 'inline' [-Winline-new-delete], err: false
 4:26.66 /home/szl/.mozbuild/clang/lib/clang/9.0.1/include/emmintrin.h:2308:19: error: use of undeclared identifier '__builtin_ia32_pavgb128', err: true
 4:26.66 /home/szl/.mozbuild/clang/lib/clang/9.0.1/include/emmintrin.h:2328:19: error: use of undeclared identifier '__builtin_ia32_pavgw128', err: true
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/nsGkAtoms.h:118:39: warning: offset of on non-standard-layout type 'mozilla::detail::GkAtoms' [-Winvalid-offsetof], err: false
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/js/Proxy.h:220:43: warning: offset of on non-standard-layout type 'js::BaseProxyHandler' [-Winvalid-offsetof], err: false
 4:26.66 /home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/dom/BrowsingContext.h:721:29: warning: offset of on non-standard-layout type 'mozilla::dom::BrowsingContext' [-Winvalid-offsetof], err: false
 4:26.66 thread 'main' panicked at 'Failed to generate bindings, flags: ["/home/szl/src/firefox/obj-x86_64-pc-linux-gnu/dist/include/GeckoProfiler.h", "--rust-target", "1.25", "--bitfield-enum", "nsChangeHint", "--bitfield-enum", "mozilla::OriginFlags", ...]', servo/components/style/build_gecko.rs:196:13
 4:26.66 stack backtrace:
 4:26.66    0:     0x5555dd797444 - backtrace::backtrace::libunwind::trace::h812748238d609e46
 4:26.66                                at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
 4:26.66    1:     0x5555dd797444 - backtrace::backtrace::trace_unsynchronized::h7c97e818aebf09c8
 4:26.66                                at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
 4:26.66    2:     0x5555dd797444 - std::sys_common::backtrace::_print_fmt::h60d914263b0ccd71
 4:26.66                                at src/libstd/sys_common/backtrace.rs:78
 4:26.66    3:     0x5555dd797444 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf78227137afc7565
 4:26.66                                at src/libstd/sys_common/backtrace.rs:59
 4:26.66    4:     0x5555dd7c228c - core::fmt::write::h543cdf60775f89bf
 4:26.66                                at src/libcore/fmt/mod.rs:1069
 4:26.66    5:     0x5555dd793963 - std::io::Write::write_fmt::h0c7f3ce24c679426
 4:26.66                                at src/libstd/io/mod.rs:1504
 4:26.66    6:     0x5555dd799ec5 - std::sys_common::backtrace::_print::h80e55e24be231368
 4:26.66                                at src/libstd/sys_common/backtrace.rs:62
 4:26.66    7:     0x5555dd799ec5 - std::sys_common::backtrace::print::h3b197b9c1261c865
 4:26.66                                at src/libstd/sys_common/backtrace.rs:49
 4:26.66    8:     0x5555dd799ec5 - std::panicking::default_hook::{{closure}}::ha6c807149ce20f8f
 4:26.66                                at src/libstd/panicking.rs:198
 4:26.66    9:     0x5555dd799c02 - std::panicking::default_hook::he49a9c12e358cc45
 4:26.66                                at src/libstd/panicking.rs:218
 4:26.67   10:     0x5555dd79a522 - std::panicking::rust_panic_with_hook::h93f74f5ef2f71f31
 4:26.67                                at src/libstd/panicking.rs:511
 4:26.67   11:     0x5555dd79a10b - rust_begin_unwind
 4:26.67                                at src/libstd/panicking.rs:419
 4:26.67   12:     0x5555dd79a07b - std::panicking::begin_panic_fmt::hfa6ef29ba81f400e
 4:26.67                                at src/libstd/panicking.rs:373
 4:26.67   13:     0x5555dd5d2b33 - build_script_build::build_gecko::generate::h32b01d9b3c12be07
 4:26.67   14:     0x5555dd5ca4cc - build_script_build::main::hfbbebcf6606b706e
 4:26.67   15:     0x5555dd5d38e3 - std::rt::lang_start::{{closure}}::hc82a9c10ef6cb231
 4:26.67   16:     0x5555dd79aa38 - std::rt::lang_start_internal::{{closure}}::h44dc84346eeba462
 4:26.67                                at src/libstd/rt.rs:52
 4:26.67   17:     0x5555dd79aa38 - std::panicking::try::do_call::h3559ee6be71549e2
 4:26.67                                at src/libstd/panicking.rs:331
 4:26.67   18:     0x5555dd79aa38 - std::panicking::try::h78f4c77016541848
 4:26.67                                at src/libstd/panicking.rs:274
 4:26.67   19:     0x5555dd79aa38 - std::panic::catch_unwind::hfe46f382761b3d41
 4:26.67                                at src/libstd/panic.rs:394
 4:26.67   20:     0x5555dd79aa38 - std::rt::lang_start_internal::he05790f0cb2000df
 4:26.67                                at src/libstd/rt.rs:51
 4:26.67   21:     0x5555dd5ca6e2 - main
 4:26.67   22:     0x7fee704bbb97 - __libc_start_main
 4:26.67   23:     0x5555dd5b602a - _start
 4:26.67   24:                0x0 - <unknown>
 4:26.67 warning: build failed, waiting for other jobs to finish...

我默认使用“开发”,而不是通常首选的“生产”,但这是个人喜好。

它是有限的,如果您想变得聪明并检查命令行参数等,请放心。这是一个起点。

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