简体   繁体   中英

Azure Blob Storage client library v12 setting proxy

I migrate from v11 to v12 and I want to replace:

var c = new CloudBlobClient(credentiels, delegatingHandler);

to

var c = new BlobServiceClient(uri, credentiels);

The problem is how to pass that the delegatingHandler in v12?

v12 azure.Storage.Blobs class name is different from v11. v12 has no DelegatingHandler parameter in the constructor.

The DelegatingHandler can change the Proxy property of the HttpClientHandler.

  • In such scenario, this instance will be unique to the client. If you don't specify a DelegatingHandler, a singleton HttpClientHandler will be used.
  • As previously said, we utilise DelegatingHandler, which has the ability to alter Proxy's properties. First, we'll build a DelegatingHandler implementation for that reason.

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

Pass this to the client's constructor.

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

REFERENCES:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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