c# - 无法在 asp.net core web API 6.0 中获取环境变量

标签 c# asp.net asp.net-core environment-variables asp.net-core-webapi

我正在使用 asp .net core web API 6.0(干净的架构项目)。

这是我的 appsettings.Production.json 和 appsettings.Development.json

  {
"Tokens": {
    "Key": "my-token",
    "Issuer": "issuer",
    "Audience": "audience",
    "ExpirationInMinutes": 1440
  },
// removed rest
}
using Microsoft.Extensions.Configuration;

    public static IServiceCollection AddTokenAuthentication(this IServiceCollection services, IConfiguration config)
    {
        var secret = config.GetValue<string>("Tokens:Key");  // this value is null while running in development / production mode. 

        var key = Encoding.ASCII.GetBytes(secret);
         // removed rest
      }

我在开发/生产环境运行时终端报错

Unhandled exception. System.ArgumentNullException: String reference not set to an instance of a String. (Parameter 's') at System.Text.Encoding.GetBytes(String s) at Infrastructure.Files.AuthenticationExtension.AddTokenAuthentication(IServiceCollection services, IConfiguration config) in /src/Infrastructure/Files/AuthenticationExtension.cs:line 14 at Infrastructure.DependencyInjection.AddInfrastructure(IServiceCollection services, IConfiguration configuration) in /src/Infrastructure/DependencyInjection.cs:line 75 at Program.<Main>$(String[] args) in/src/WebApi/Program.cs:line 21 Aborted (core dumped)

注意: 如果我像这样硬编码 secret 的值 var secret = "my-token"; 那么项目在两个环境中都运行良好。

在开发和生产环境中运行时,所有环境变量均为空。

GetConnectionString 的相同错误

public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {

        services.AddDbContext<DbContext>(options =>
            options.UseNpgsql(
                configuration.GetConnectionString("DefaultConnection"),  // this is also null while running in both environment.. If I hard-coded then working fine
                b => b.MigrationsAssembly(typeof(DbContext).Assembly.FullName))
                .EnableSensitiveDataLogging());
// removed rest
}

为什么会这样? 我的代码有错误吗? 请帮我找出错误?

编辑: 我也试过从 appsettings.Development.json 复制所有内容并将其粘贴到 appsettings.json 那是行不通的。

这是我的 启动设置.json

"WebApi": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": false,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApi-Production": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },

最佳答案

生产环境是否设置环境变量ASPNETCORE_ENVIRONMENT=Production?

默认情况下,ASP.NET Core 将从 appsettings.json 文件和 appsettings.Environment.json 文件(例如 appsettings.Development.json)读取配置。

您可以在生产环境中设置 ASPNETCORE_ENVIRONMENT=Production 或在 appsettings.json 文件中设置生产配置。

这些文档可能对您有帮助:

----------------例如------------
我的 appsettings.json

{
    "EFCoreSlowQuery": {
        "ServiceName": "json",
        "SlowQueryThresholdMilliseconds": 10
    }
}

在代码中读取配置:
enter image description here

关于c# - 无法在 asp.net core web API 6.0 中获取环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71206350/

相关文章:

asp.net - 静态字段初始值设定项有时会在 Application_start 之前运行

asp.net - 格式验证摘要

docker - Asp.net 5- docker

c# - 断点不会在 Blazor Webassembly 项目、ASP.NET Core 3.1、

c# - 如何在没有空异常的情况下使用 linq2Xml?

c# - 创建通用扩展方法时出现问题

c# - ASP.NET 多层应用程序迁移到 ASP.NET Core

C# OWIN 安全 - 设置过期 token - 始终具有默认值

asp.net - Application_Start 在工作站上工作正常,部署时不会被调用

asp.net-mvc - Identity Server 4 将参数添加到登录页面