.NET Core - 能够连接到 Azure App Config 但无法返回任何配置值? [英] .NET Core - Able to connect to Azure App Config but can't return any config values?

查看:35
本文介绍了.NET Core - 能够连接到 Azure App Config 但无法返回任何配置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET Core Azure Function 应用程序,我在其中连接到 Azure App Config 服务并注册 Key Vault 连接:

I have a .NET Core Azure Function app where I am connecting to an Azure App Config service and registering a Key Vault connection:

using Azure.Identity;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using System;

[assembly: FunctionsStartup(typeof(Func.Accounts.Api.Startup))]

namespace Func.Accounts.Api
{
    public class Startup : FunctionsStartup
    {
        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            string connectionString = Environment.GetEnvironmentVariable("AppConfigConnectionString");

            builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
            {
                options.Connect(connectionString)
                    .ConfigureKeyVault(kv =>
                    {
                        kv.SetCredential(new DefaultAzureCredential());
                    });
            });
        }

        public override void Configure(IFunctionsHostBuilder builder)
        {
        }
    }
}

我可以正常连接到服务,我可以通过 Azure 门户看到它正在接收请求,但是我无法让它实际返回任何配置值.这是配置注入函数后的样子:

I can connect to the service fine, and I can see through the Azure portal that it is receiving requests however I cannot get it to actually return any config values. This is what the config looks like after its been injected into the function:

我知道 App Config Service 中有 3 个配置值,但它没有返回任何数据,而且我看不到它在哪里失败.据我所知,不存在身份验证问题,而且我已经使用相同的方法在不同的项目中成功实现了这一点.

I know there are 3 config values in the App Config Service but it does not return any data and I cannot see where it is failing. As far as I can tell there isn't an authentication issue and I have achieved this successfully in a different project using the same method.

有趣的是,如果我尝试通过 Azure 应用配置客户端库 检索密钥,我会在 GetConfigurationSetting 方法上收到以下 404 错误.我很难理解它是如何看不到我的任何配置设置的.

Interestingly, if I try to retrieve the key through the Azure App Configuration client library I get the following 404 error on the GetConfigurationSetting method. I'm struggling to understand how it doesn't see any of my config settings.

推荐答案

从评论看来,你需要做两件事:

From the comments, it looks like you need to do two things:

  1. 如果您使用标签,则需要在配置应用程序时为其添加过滤器.
  2. 如果您使用 Azure Key Vault,请确保您授予对应用配置的访问权限

这是使用标签加载配置的示例.

Here's an example of loading the configuration using labels.

[assembly: FunctionsStartup(typeof(Host.Example.Startup))]

namespace Host.Example
{
    public partial class Startup : FunctionsStartup
    {
        public IConfiguration Configuration { get; set;  }

        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            var config = builder.ConfigurationBuilder.Build();

            var appConfigConnection = config.GetConnectionString("AppConfig");
            // Here the label is the environment name
            var environment = Environment.GetEnvironmentVariable("Environment");

            builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
            {
                // Load the configuration using labels
                // Connect it to the Key Vault. Didn't test the Key Vault code but it should work if configured properly
                options.Connect(appConfigConnection)
                    .Select(KeyFilter.Any, environment.ToLowerInvariant())
                    .ConfigureKeyVault(kv =>
                    {
                        kv.SetCredential(new DefaultAzureCredential());
                    });
            });
        }

        public override void Configure(IFunctionsHostBuilder builder)
        {
            Configuration = builder.GetContext().Configuration;
        }
    }
}

这篇关于.NET Core - 能够连接到 Azure App Config 但无法返回任何配置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆