ASP.NET Core için Azure istemci kitaplığı tümleştirmesi

Microsoft.Extensions.Azure, Azure istemcilerini ASP.NET Core bağımlılık ekleme ve yapılandırma sistemleriyle tümleştirmek için paylaşılan temel öğeler sağlar.

Kaynak kodu | Paket (NuGet)

Başlarken

Paketi yükleme

NuGet kullanarak ASP.NET Core tümleştirme kitaplığını yükleyin:

dotnet add package Microsoft.Extensions.Azure

İstemcileri kaydetme

AddAzureClients Uygulamanızın ConfigureServices yönteminde çağrısı yapın. Sağlanan oluşturucuyu kullanarak istemci örneklerini bağımlılık ekleme kapsayıcınıza kaydedebilirsiniz.

public void ConfigureServices(IServiceCollection services)
{
    // Registering policy to use in ConfigureDefaults later
    services.AddSingleton<DependencyInjectionEnabledPolicy>();

    services.AddAzureClients(builder => {
        // Register blob service client and initialize it using the KeyVault section of configuration
        builder.AddSecretClient(Configuration.GetSection("KeyVault"))
            // Set the name for this client registration
            .WithName("NamedBlobClient")
            // Set the credential for this client registration
            .WithCredential(new ClientSecretCredential("<tenant_id>", "<client_id>", "<client_secret>"))
            // Configure the client options
            .ConfigureOptions(options => options.Retry.MaxRetries = 10);

        // Adds a secret client using the provided endpoint and default credential set later
        builder.AddSecretClient(new Uri("http://my.keyvault.com"));

        // Configures environment credential to be used by default for all clients that require TokenCredential
        // and doesn't override it on per registration level
        builder.UseCredential(new EnvironmentCredential());

        // This would use configuration for auth and client settings
        builder.ConfigureDefaults(Configuration.GetSection("Default"));

        // Configure global retry mode
        builder.ConfigureDefaults(options => options.Retry.Mode = RetryMode.Exponential);

        // Advanced configure global defaults
        builder.ConfigureDefaults((options, provider) => options.AddPolicy(provider.GetService<DependencyInjectionEnabledPolicy>(), HttpPipelinePosition.PerCall));

        // Register blob service client and initialize it using the Storage section of configuration
        builder.AddBlobServiceClient(Configuration.GetSection("Storage"))
                .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);
    });
}

İstemcileri ekleme

İstemciyi kullanmak için, bağımlılık eklemeyi destekleyen herhangi bir yerden istemci türünü isteyin (oluşturucular, Çağrıları yapılandırma, @inject razor tanımları vb.)

public void Configure(IApplicationBuilder app, SecretClient secretClient, IAzureClientFactory<BlobServiceClient> blobClientFactory)

Adlandırılmış örnekler oluşturma

İstemci adlandırılmış istemci olarak kaydedildiyse adı geçirerek ekleme IAzureClientFactory<T> ve çağırma CreateClient :

BlobServiceClient blobServiceClient = blobClientFactory.CreateClient("NamedBlobClient");

Yukarıdaki örnekte kullanılan yapılandırma dosyası:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "AllowedHosts": "*",
  "Default": {
    "ClientId": "<client_id>",
    "ClientSecret": "<client_secret>",
    "TenantId": "<tenant_id>",

    "TelemetryPolicy": {
      "ApplicationId": "AppId"
    }
  },
  "KeyVault": {
    "VaultUri": "<vault_uri>"
  },
  "Storage": {
    "serviceUri": "<service_uri>",
    "credential": {
      "accountName": "<account_name>",
      "accountKey": "<account_key>"
    }
  }
}

Özel istemci fabrikasını kaydetme

İstemci örneğinin nasıl oluşturulduğunu denetlemek istiyorsanız veya istemci oluşturma sırasında diğer bağımlılıkları kullanmanız gerekiyorsa yöntemini kullanın AddClient<TClient, TOptions> .

İstemciyi oluşturmak için örneği kullanma IOptions<T> örneği aşağıda verilmiştir:

public class MyApplicationOptions
{
    public Uri KeyVaultEndpoint { get; set; }
}

public void ConfigureServices(IServiceCollection services)
{
    // Configure a custom options instance
    services.Configure<MyApplicationOptions>(options => options.KeyVaultEndpoint = new Uri("http://localhost/"));

    services.AddAzureClients(builder =>
    {
        // Register a client using MyApplicationOptions to get constructor parameters
        builder.AddClient<SecretClient, SecretClientOptions>((options, credential, provider) =>
        {
            var appOptions = provider.GetService<IOptions<MyApplicationOptions>>();
            return new SecretClient(appOptions.Value.KeyVaultEndpoint, credential, options);
        });
    });
}

Katkıda bulunma

Bu proje, katkı ve önerilere açıktır. Çoğu durumda, sağladığınız katkıyı kullanmamız için bize hak tanıma hakkına sahip olduğunuzu ve bu hakkı bize tanıdığınızı bildiren bir Katkıda Bulunan Lisans Sözleşmesi’ni (CLA) kabul etmeniz gerekir. Ayrıntılar için bkz. https://cla.microsoft.com.

Bir çekme isteği gönderdiğinizde, CLA robotu bir CLA sağlamanız gerekip gerekmediğini otomatik olarak belirler ve çekme isteğini uygun şekilde donatır (örn. etiket, açıklama). Robot tarafından sağlanan yönergeleri izlemeniz yeterlidir. Bu işlemi, CLA’mızı kullanarak tüm depolarda yalnızca bir kere yapmanız gerekir.

Bu proje Microsoft Open Source Code of Conduct (Microsoft Açık Kaynak Kullanım Kuralları) belgesinde listelenen kurallara uygundur. Daha fazla bilgi için Kullanım Kuralları SSS bölümüne bakın veya ek sorularınız veya yorumlarınızla iletişime geçin opencode@microsoft.com .