gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:03:51 25 4
gpt4 key购买 nike

我正在使用 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/

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