【问题标题】:Azure Blob Storage client library v12 setting proxyAzure Blob 存储客户端库 v12 设置代理
【发布时间】:2022-01-07 23:58:51
【问题描述】:

我从 v11 迁移到 v12,我想替换:

var c = new CloudBlobClient(credentiels, delegatingHandler);

var c = new BlobServiceClient(uri, credentiels);

问题是如何在 v12 中传递 delegatingHandler

【问题讨论】:

    标签: c# proxy azure-blob-storage


    【解决方案1】:

    v12 azure.Storage.Blobs 类名与 v11 不同。 v12 的构造函数中没有 DelegatingHandler 参数。

    DelegatingHandler 可以改变 HttpClientHandler 的 Proxy 属性。

    • 在这种情况下,此实例对于客户端是唯一的。如果不指定 DelegatingHandler,将使用单例 HttpClientHandler。
    • 如前所述,我们使用 DelegatingHandler,它能够更改代理的属性。首先,出于这个原因,我们将构建一个 DelegatingHandler 实现。

    ProxyInjectionHandler.cs

    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Hoge {
      public class ProxyInjectionHandler: DelegatingHandler {
        private readonly IWebProxy Proxy;
        private bool FirstCall = true;
    
        public ProxyInjectionHandler(IWebProxy proxy) {
          this.Proxy = proxy;
        }
    
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
          if (FirstCall) {
            var handler = (HttpClientHandler) this.InnerHandler;
            handler.Proxy = this.Proxy;
            handler.UseProxy = true;
            FirstCall = false;
          }
          return base.SendAsync(request, cancellationToken);
        }
      }
    }
    

    将此传递给客户端的构造函数。

    var account = CloudStorageAccount.Parse (connectionString);  
    var client = new CloudBlobClient (account.BlobEndpoint, account.Credentials, new ProxyInjectionHandler (new WebProxy (new Uri (proxyUrl))));  
    var container = client.GetContainerReference ("...");  
    await container.CreateIfNotExistsAsync ();
    

    参考:

    【讨论】:

    • 您好,感谢您的解释,问题是如何将其应用于 v12 版:D 这正是我要问的情况。
    猜你喜欢
    • 2020-08-22
    • 2022-12-15
    • 2016-06-13
    • 2021-05-21
    • 2019-03-21
    • 2016-10-27
    • 2016-09-29
    • 2017-08-07
    • 2020-01-10
    相关资源
    最近更新 更多