[英]Blocking connection to Azure storage account
我有一个用 c# 开发的应用程序,它的第一个功能是连接到存储帐户以便能够管理 blob 的方法。 我的问题是我想在尝试连接 3 篇文章后阻止连接。
这是表示与存储帐户的连接的方法
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;
}
}
在您创建CloudStorageAccount
变量时,仍然没有与存储帐户建立连接,您可以通过添加随机凭据轻松测试。 在后台,该库所做的只是向 Storage API 发出 REST 调用,因此在您实际检索或发送数据之前不会建立任何连接。
该库也已经实现了自己的机制来在失败的情况下重试请求,默认为 3 次重试,但您可以像这样手动更改:
var options = new BlobRequestOptions()
{
RetryPolicy = new ExponentialRetry(deltaBackoff, maxAttempts),
};
cloudBlobClient.DefaultRequestOptions = options;
将它包装在一个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.