无论如何在 azure 函数中使用 AddSecretClient DI 扩展?

Is there anyway to use the AddSecretClient DI extension in an azure function?

我正在尝试为我的 azure 函数配置一些依赖项。我需要能够访问(除其他外)天蓝色的密钥库。目前我正在手动访问它并且必须自己做所有的依赖注入。这感觉不对,我开始寻找更好的方法来连接它。我发现 this blog 看起来很理想。

public void ConfigureServices(IServiceCollection services)
{
  services.AddAzureClients(builder =>
  {
    // Add a KeyVault client
    builder.AddSecretClient(keyVaultUrl);

    // Add a storage account client
    builder.AddBlobServiceClient(storageUrl);

    // Use the environment credential by default
    builder.UseCredential(new EnvironmentCredential());
  });

  services.AddControllers();
}

太好了,我想这样做。问题是这些扩展似乎不支持在 Azure 函数中实现的特定 DI。具体来说,AddSecretClient 的预期类型与注入 Configure(IFunctionsHostBuilder builder) 的构建器不兼容:

[assembly: FunctionsStartup(typeof(Startup))]
namespace Snapshot.Take
{
    [ExcludeFromCodeCoverage]
    public class Startup : FunctionsStartup
    {
        

        public override void Configure(IFunctionsHostBuilder builder)
        {
            RegisterHttpClients(builder);

            builder.Services.AddLogging();

            //error

            builder.AddSecretClient(new Uri(""));
       }
   }
}

The type 'Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder' cannot be used as type parameter 'TBuilder' in the generic type or method 'SecretClientBuilderExtensions.AddSecretClient(TBuilder, Uri)'. There is no implicit reference conversion from 'Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder' to 'Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential'.

那么是否有这些扩展的 Azure 功能版本,或者我必须自己推出?

由于 AddAzureClientsIServiceCollection 上的扩展方法, 您可能需要执行以下操作:

  builder.Services.AddAzureClients(clientBuilder =>
  {
    // Add a KeyVault client
    clientBuilder.AddSecretClient(keyVaultUrl);

    // Add a storage account client
    clientBuilder.AddBlobServiceClient(storageUrl);

    // Use the environment credential by default
    clientBuilder.UseCredential(new EnvironmentCredential());
  });