为什么ubuntu中的NET Core 3.1 BackgroundWorker无法访问环境变量?

问题描述 投票:0回答:1

我有一个.net core 3.1后台工作程序,作为后台工作程序安装在ubuntu中。但是它无法获取我需要进行多配置的环境变量的值,这就是为什么您可以看到主机环境的值是默认值,即Production。

我已经尝试通过hostenvironment.environmentname和Environment.GetEnvironmentVariable来获取它。

/ etc / systemd /中的我的服务文件

[Unit]
Description=TestService

[Service]
Type=notify
WorkingDirectory=/home/centos/TestService/
ExecStart=/usr/bin/dotnet /home/centos/TestService/WorkerService2.dll
User=centos

[Install]
WantedBy=multi-user.target

。NET Core 3.1后台工作程序中的代码。

 _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
            _logger.LogError("ERROR testing");
            _logger.LogInformation($"Hosting Enviornment: {_hostEnvironment.EnvironmentName}");
            _logger.LogInformation(_configuration["Test"]);
            var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var contentroothpath = _hostEnvironment.ContentRootPath;

            Console.WriteLine($"basepath = {basePath}");
            Console.WriteLine($"contentrootpath = {contentroothpath}");
            _logger.LogInformation($"basepath = {basePath}");
            _logger.LogInformation($"contentrootpath = {contentroothpath}");



            var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var environmentName2 = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");

            _logger.LogInformation($"ASPNETCORE_ENVIRONMENT = {environmentName}");
            _logger.LogInformation($"DOTNET_ENVIRONMENT = {environmentName2}");

cmd输出。

enter image description hereenter image description here

program.cs中的代码

 public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSystemd()
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
                var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                //NLog.LogManager.LoadConfiguration($"{basePath}/NLog.{hostContext.HostingEnvironment.ContentRootPath}.config");
            })
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile(
                    $"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
                //NLog.LogManager.LoadConfiguration($"/home/centos/TestService/{hostingContext.HostingEnvironment.EnvironmentName}.config");
                //NLog.LogManager.LoadConfiguration($"{basePath}" +
                //    $"/NLog.{hostingContext.HostingEnvironment.EnvironmentName}.config");
            });
c# linux environment-variables backgroundworker .net-core-3.0
1个回答
0
投票

您将需要在Main()中访问HostBuilderContext来访问托管环境,如下所示:>

 CreateHostBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var env = hostingContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

                if (env.IsDevelopment() || env.EnvironmentName.ToLower() == "dev")
                    config.AddJsonFile("appsettings.dev.json", optional: true, reloadOnChange: true);
                else if (env.IsStaging() || env.EnvironmentName.ToLower() == "stage")
                    config.AddJsonFile("appsettings.stage.json", optional: true, reloadOnChange: true);                    

                config.AddEnvironmentVariables();
            }).Build().Run();
© www.soinside.com 2019 - 2024. All rights reserved.