简体   繁体   English

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

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

I'm trying to configure some depedancies for my azure function.我正在尝试为我的 azure 函数配置一些依赖项。 I need to be able to access (among other things) an azure keyvault.我需要能够访问(除其他外)一个天蓝色的密钥库。 Currently I'm accessing this manually and having to do all my own dependancy injection.目前我正在手动访问它并且不得不做我自己的所有依赖注入。 This didn't feel right and I went looking for a better way to hook this up.这感觉不对,我开始寻找更好的方法来解决这个问题。 I found this blog that seems ideal.我发现这个博客看起来很理想。

 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(); }

Great I want to do that.太好了,我想这样做。 Problem is these extensions don't seem to support the particular DI implemented in Azure functions.问题是这些扩展似乎不支持在 Azure 函数中实现的特定 DI。 Specifically there is an incompatibility between the type expected for the AddSecretClient and the builder injected into the Configure(IFunctionsHostBuilder builder) :具体来说, 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)'.类型“Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder”不能用作泛型类型或方法“SecretClientBuilderExtensions.AddSecretClient(TBuilder, Uri)”中的类型参数“TBuilder”。 There is no implicit reference conversion from 'Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder' to 'Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential'.没有从“Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder”到“Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential”的隐式引用转换。

So is there an Azure function version of these extensions or do I have to roll my own?那么是否有这些扩展的 Azure 功能版本,还是我必须自己推出?

Since AddAzureClients is an extension method on IServiceCollection , you'll probably need to do something like:由于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());
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM