简体   繁体   中英

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. My problem is that I want to block connection after 3 essaies of trying to connect.

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. 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.

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:

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?

string strError;
const int maxConnectionAttempts = 3;

var connectionAttempts = 0;
var connected = false;

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

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