如何在服务上配置HttpProxy,AddAzureClients

siotufzp  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(98)

在. Net5.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")
            });
wnavrhmk

wnavrhmk1#

解决方案是将指定了适当代理URI的BlobClientOptions传递到BlobServiceClient的构造函数中。
查看下面的示例代码,它展示了如何在HttpClient中指定代理
在构造新的HttpClientTransport时,HttpClient可以作为参数传递,该参数可以在BlobClientOption的Transport属性中设置,然后可以将其传递到BlobServiceClient的构造函数中。

8qgya5xd

8qgya5xd2#

var blobServiceClient = new BlobServiceClient(_serviceUri, new 
AzureSasCredential(_sasToken), new BlobClientOptions()     
{
     Transport = new HttpClientTransport(new HttpClient(new HttpClientHandler 
     {
        Proxy = new WebProxy("proxy-url:port", BypassOnLocal: true),
        UseProxy = true
    }))
});

相关问题