无法在ASP.NET核心Web API 6.0中获取环境变量

编程入门 行业动态 更新时间:2024-10-28 18:22:46
本文介绍了无法在ASP.NET核心Web API 6.0中获取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.Net核心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中 这不起作用。

这是我的 LaunchSettings.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 ment.json文件(例如appsettings.Development.json)读取配置。

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

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

Configuration in ASP.NET Core Use multiple environments in ASP.NET Core

-例如-
我的appsettings.json

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

读取代码中的配置:

这篇关于无法在ASP.NET核心Web API 6.0中获取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-13 15:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/616394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境变量   核心   NET   ASP   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!