c# - .NET Core 2.2 - 无法从 Azure AppConfig 获取应用程序配置

标签 c# azure app-config app-startup azure-app-configuration

我已经创建了一个 Azure AppConfiguration 存储并获取了访问 key (EndPoint、Id、Secret、ConnectionString)。

现在,我想从我的 .NET Core WebAPI 应用程序读取 Azure 中的配置。我尝试了很多网上找到的方法,但都不起作用。

我已经添加了对所需 Nuget 库的引用。 我的appsettings.json:

{
    "ConnectionString:AppConfig": "Endpoint=https://somewhere.azconfig.io;Id=Hidden;Secret=Hidden",
    "EndPoint": "https://somewhere.azconfig.io"
}

在我的 Program.cs 中,我有:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        var settings = config.Build();

        config.AddAzureAppConfiguration(options =>
        {
            options.Connect(settings["ConnectionStrings:AppConfig"]);
        });
    })
    .UseStartup<Startup>();

然后在我的 Startup.cs 构造函数中:

public Startup(
    IConfiguration configuration, ---> checked this but no Azure config data
    IHostingEnvironment hostingEnv
)
    {
        var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        //this.Config = configuration;
        var builder = new ConfigurationBuilder()
                    .SetBasePath(hostingEnv.ContentRootPath)
                    .AddJsonFile(...)
                    .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

这是我读取配置的地方,但所有值都是null

public void ConfigureServices(IServiceCollection services)
    {
        var dbConnection = Configuration.GetSection("MyApp:Key1:Key2").Value; ---> get null
        services.AddDbContext<MyDbContext>(options => options.UseSqlServer(dbConnection));

        _jwtLifespan = Configuration.GetValue<int>("MyApp:Key3"); ----> get null
        _jwtSecretKey = Configuration.GetSection("MyApp:Key4").Value; ----> get null

        .....
    }

任何人请检查我在这里缺少什么并建议我一个可行的解决方案。非常感谢!

最佳答案

假设这是您的连接设置问题。从您的代码中您正在使用 settings["ConnectionStrings:AppConfig"]获取连接字符串,但是您正在使用 appsettings.json

来自教程:Connect to an app configuration store ,如果您想使用settings["ConnectionStrings:AppConfig"] ,使用dotnet user-secrets set ConnectionStrings:AppConfig <your_connection_string>将您的连接字符串添加到 Secret Manager。

注意: secret 管理器仅用于本地测试,当应用程序部署到 Azure 应用服务时,请使用应用服务中的应用程序设置连接字符串。

如果你想使用appsettings.json ,你可以尝试下面的代码。

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((hostingContext, config) =>
    {

        var builder = new ConfigurationBuilder()

            .SetBasePath(Environment.CurrentDirectory)

            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)

            .AddEnvironmentVariables();

        var Configuration = builder.Build();
        var settings = config.Build();
        config.AddAzureAppConfiguration(Configuration.GetSection("ConnectionStrings:AppConfig").Value);
    })
    .UseStartup<Startup>();

然后我使用教程示例来显示 AppConfiguration 中的值。

enter image description here

enter image description here

希望这可以帮助您,如果您还有其他问题,请随时告诉我。

关于c# - .NET Core 2.2 - 无法从 Azure AppConfig 获取应用程序配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496039/

相关文章:

c# - 用正则表达式分割

c# - 有没有办法简单地类似于真值表?

c# - 对象与对象的区别

azure - 如何保护我的 Azure Web 角色免受其他人的侵害?

azure - 如何使用 Azure DevOps 工件存储库作为 AzureML DatabricksStep 的源?

.net - 如何让ConfigurationManager读取app.config以外的配置文件?

c# - UnauthorizedAccessException app.config c#

c# - .Find 方法能否在多用户环境中返回错误结果

azure - 同一应用服务中的 Azure 函数是否在同一实例中运行

Winforms ClickOnce 发布因 app.config 转换而失败