首页 > 解决方案 > 我们如何在 services.AddAzureClients 上配置 HttpProxy

问题描述

我们的 .Net 5.0 aspnetcore 启动类中有以下代码

public void ConfigureServices(IServiceCollection services)
{
  services.AddAzureClients(builder =>
  {
 
    // Add a storage account client
    builder.AddBlobServiceClient(storageUrl);
    // Use the environment credential by default
    builder.UseCredential(new EnvironmentCredential());
  });

  services.AddControllers();
}

对于 HttpClient,我们可以使用以下代码配置 httpproxy,但是我们如何为 BlobServiceClient 实现相同的功能?

  services.AddHttpClient("SampleClient", client =>
                {
                    client.BaseAddress = new Uri("https://sample.client.url.com");
                })
            .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
            {
                Proxy = new WebProxy("https://sample.proxy.url.com")
            });

标签: azureasp.net-coreasp.net-web-apiazure-blob-storage

解决方案


The solution is to pass BlobClientOptions with the appropriate proxy URI specified, into the constructor of BlobServiceClient.

Checkout this sample code which shows how specify a proxy in a HttpClient

The HttpClient can be passed as a parameter when constructing a new HttpClientTransport, which can be set in the Transport property of BlobClientOption, which can then be passed into the constructor of BlobServiceClient.


推荐阅读