简体   繁体   English

阻止连接到 Azure 存储帐户

[英]Blocking connection to Azure storage account

I have an application developed with c# which the first functionality is a method that connect to a storage account in order to be able to manage blobs.我有一个用 c# 开发的应用程序,它的第一个功能是连接到存储帐户以便能够管理 blob 的方法。 My problem is that I want to block connection after 3 essaies of trying to connect.我的问题是我想在尝试连接 3 篇文章后阻止连接。

this is the method that represent the connection to the storage account这是表示与存储帐户的连接的方法

public bool Connect(out String strerror)
{
    strerror = "";

    try
    {
        storageAccount = new CloudStorageAccount(new StorageCredentials(AccountName, AccountConnectionString), true);
        MSAzureBlobStorageGUILogger.TraceLog(MessageType.Control,CommonMessages.ConnectionSuccessful);
        return true;
    }
    catch (Exception ex01)
    {
        Console.WriteLine(CommonMessages.ConnectionFailed + ex01.Message);
        strerror =CommonMessages.ConnectionFailed +ex01.Message;
        return false;
    }

}

At the moment you create the CloudStorageAccount variable there's still no connection made to the Storage Account, which you can easily test out by adding random credentials.在您创建CloudStorageAccount变量时,仍然没有与存储帐户建立连接,您可以通过添加随机凭据轻松测试。 In the background all the library does is fire a REST call to the Storage API and therefore doesn't make any connection until you actually retrieve or send data.在后台,该库所做的只是向 Storage API 发出 REST 调用,因此在您实际检索或发送数据之前不会建立任何连接。

The library also already has its own mechanism implemented to retry requests in case of failures, which defaults to 3 retries but you can change manually like this:该库也已经实现了自己的机制来在失败的情况下重试请求,默认为 3 次重试,但您可以像这样手动更改:

var options = new BlobRequestOptions()
{
  RetryPolicy = new ExponentialRetry(deltaBackoff, maxAttempts),
};
cloudBlobClient.DefaultRequestOptions = options;

What about wrapping it in a while loop and continuing to retry until either success or hitting the 3 attempt maximum?将它包装在一个while循环中并继续重试直到成功或达到 3 次尝试的最大值呢?

string strError;
const int maxConnectionAttempts = 3;

var connectionAttempts = 0;
var connected = false;

while (!connected && connectionAttempts < maxConnectionAttempts) 
{
    connected = Connect(out strError);
    connectionAttempts++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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