无论如何,是否可以在 azure 函数中使用 AddSecretClient DI 扩展?

Lia*_*iam 2 c# dependency-injection azure nuget-package azure-functions

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

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();
}
Run Code Online (Sandbox Code Playgroud)

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

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();
}
Run Code Online (Sandbox Code Playgroud)

类型“Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder”不能用作泛型类型或方法“SecretClientBuilderExtensions.AddSecretClient(TBuilder, Uri)”中的类型参数“TBuilder”。没有从“Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder”到“Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential”的隐式引用转换。

那么这些扩展是否有 Azure 函数版本,还是我必须自己推出?

juu*_*nas 7

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

  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());
  });
Run Code Online (Sandbox Code Playgroud)

  • 对于后来的任何人,可以在包“Microsoft.Extensions.Azure”中找到“AddAzureClients()” (12认同)