本文整理汇总了C#中IRetryPolicy.ShouldRetry方法的典型用法代码示例。如果您正苦于以下问题:C# IRetryPolicy.ShouldRetry方法的具体用法?C# IRetryPolicy.ShouldRetry怎么用?C# IRetryPolicy.ShouldRetry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IRetryPolicy
的用法示例。
在下文中一共展示了IRetryPolicy.ShouldRetry方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UploadFileToBlob
//.........这里部分代码省略.........
// move the file system reader to the proper position
fs.Seek(blockIdAndLength.Key * (long)maxBlockSize, SeekOrigin.Begin);
int readSize = binaryReader.Read(buffer, 0, blockIdAndLength.Value);
if (fileEncryption != null)
{
lock (fileEncryption)
{
using (FileEncryptionTransform encryptor = fileEncryption.GetTransform(file.Name, blockIdAndLength.Key * (long)maxBlockSize))
{
encryptor.TransformBlock(buffer, 0, readSize, buffer, 0);
}
}
}
using (var ms = new MemoryStream(buffer, 0, blockIdAndLength.Value))
{
string blockIdString = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture, "BlockId{0}", blockIdAndLength.Key.ToString("0000000", CultureInfo.InvariantCulture))));
string blockHash = GetMd5HashFromStream(buffer);
if (blob != null) blob.PutBlock(blockIdString, ms, blockHash, options: options);
}
Interlocked.Add(ref bytesSent, blockIdAndLength.Value);
var progress = (int)((double)bytesSent / file.Length * 100);
var eArgs = new BlobTransferProgressChangedEventArgs(bytesSent, blockIdAndLength.Value, file.Length, progress, _uploadSpeedCalculator.UpdateCountersAndCalculateSpeed(bytesSent), uri, localFile, null);
OnTaskProgressChanged(eArgs);
}
catch (StorageException ex)
{
TimeSpan tm;
exceptionCount++;
exceptions.Add(ex);
if (!retryPolicy.ShouldRetry(exceptions.Count, ex.RequestInformation.HttpStatusCode, ex, out tm, new OperationContext()))
{
lastException = new AggregateException(String.Format(CultureInfo.InvariantCulture, "Received {0} exceptions while uploading. Canceling upload.", exceptions.Count), exceptions);
throw lastException;
}
Thread.Sleep(tm);
queue.Enqueue(blockIdAndLength);
}
catch (IOException ex)
{
TimeSpan tm;
exceptionCount++;
exceptions.Add(ex);
if (!retryPolicy.ShouldRetry(exceptions.Count, 0, ex, out tm, new OperationContext()))
{
lastException = new AggregateException(String.Format(CultureInfo.InvariantCulture, "Received {0} exceptions while reading file {1} @ location {2} to be uploaded. Canceling upload.",
exceptions.Count, file.Name, blockIdAndLength.Key * (long)maxBlockSize), exceptions);
throw lastException;
}
// dispose existing file stream
if (fs != null)
{
fs.Close();
}
Thread.Sleep(tm);
// try to reopen the file stream again
fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
queue.Enqueue(blockIdAndLength);
}
示例2: InitializeCloudBlockBlob
private static CloudBlockBlob InitializeCloudBlockBlob(Uri uri, CloudBlobClient client, IRetryPolicy retryPolicy)
{
CloudBlockBlob blob = null;
if (client != null)
{
blob = new CloudBlockBlob(uri, client.Credentials);
}
else
{
blob = new CloudBlockBlob(uri);
}
bool fetch = false;
bool shouldRetry = true;
TimeSpan delay;
int retryCount = 0;
StorageException lastException = null;
while (!fetch && shouldRetry)
{
try
{
blob.FetchAttributes(options: new BlobRequestOptions() { RetryPolicy = retryPolicy });
fetch = true;
}
catch (StorageException ex)
{
retryCount++;
lastException = ex;
shouldRetry = retryPolicy.ShouldRetry(retryCount, ex.RequestInformation.HttpStatusCode, lastException, out delay, new OperationContext());
Thread.Sleep(delay);
}
}
if (!fetch)
{
throw lastException;
}
return blob;
}
示例3: DownloadFileFromBlob
private void DownloadFileFromBlob(Uri uri, string localFile, FileEncryption fileEncryption, ulong initializationVector, CloudBlobClient client, CancellationToken cancellationToken, IRetryPolicy retryPolicy)
{
int numThreads = ParallelTransferThreadCount;
List<Exception> exceptions = new List<Exception>();
AggregateException aggregateException = null;
long bytesDownloaded = 0;
CloudBlockBlob blob = InitializeCloudBlockBlob(uri, client, retryPolicy);
long blobLength = blob.Properties.Length;
int bufferLength = GetBlockSize(blobLength);
var queue = PrepareDownloadQueue(blobLength, bufferLength, ref numThreads);
if (cancellationToken.IsCancellationRequested)
{
TaskCompletedCallback(true, null, BlobTransferType.Download, localFile, uri);
cancellationToken.ThrowIfCancellationRequested();
}
using (var fs = new FileStream(localFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
{
var tasks = new List<Task>();
Action action = () =>
{
KeyValuePair<long, int> blockOffsetAndLength;
int exceptionPerThread = 0;
// A buffer to fill per read request.
var buffer = new byte[bufferLength];
if (_forceSharedAccessSignatureRetry != TimeSpan.Zero)
{
// The following sleep is for unit test purpose and we will force the shared access signature to expire and hit retry code path
Thread.Sleep(_forceSharedAccessSignatureRetry);
}
while (queue.TryDequeue(out blockOffsetAndLength))
{
if (cancellationToken.IsCancellationRequested)
{
break;
}
try
{
var blobGetRequest = BlobGetRequest(blockOffsetAndLength, blob);
using (var response = blobGetRequest.GetResponse() as HttpWebResponse)
{
if (response != null)
{
ReadResponseStream(fileEncryption, initializationVector, fs, buffer, response, blockOffsetAndLength, ref bytesDownloaded);
var progress = (int)((double)bytesDownloaded / blob.Properties.Length * 100);
// raise the progress changed event
var eArgs = new BlobTransferProgressChangedEventArgs(bytesDownloaded, blockOffsetAndLength.Value, blob.Properties.Length, progress, _downloadSpeedCalculator.UpdateCountersAndCalculateSpeed(bytesDownloaded), uri, localFile, null);
OnTaskProgressChanged(eArgs);
}
}
}
catch (Exception ex)
{
var webEx = ex as WebException;
bool ok = (webEx != null) || ex is ObjectDisposedException;
if (!ok)
{
throw;
}
if (webEx != null)
{
if (webEx.Response is HttpWebResponse)
{
var httpex = (HttpWebResponse)webEx.Response;
if (httpex.StatusCode == HttpStatusCode.Forbidden)
{
blob = InitializeCloudBlockBlob(uri, null, retryPolicy);
}
}
}
TimeSpan tm;
exceptionPerThread++;
exceptions.Add(ex);
if (!retryPolicy.ShouldRetry(exceptionPerThread, 0, ex, out tm, new OperationContext()))
{
aggregateException = new AggregateException(String.Format(CultureInfo.InvariantCulture, "Received {0} exceptions while downloading. Canceling download.", exceptions.Count), exceptions);
throw aggregateException;
}
Thread.Sleep(tm);
// Add block back to queue
queue.Enqueue(blockOffsetAndLength);
}
}
};
// Launch threads to download chunks.
for (int idxThread = 0; idxThread < numThreads; idxThread++)
{
tasks.Add(Task.Factory.StartNew(action));
//.........这里部分代码省略.........
示例4: VerifyBackoffTimeOverflow
private void VerifyBackoffTimeOverflow(IRetryPolicy retryPolicy, int maxAttempts)
{
StorageException e = new StorageException();
OperationContext context = new OperationContext();
TimeSpan retryInterval;
TimeSpan previousRetryInterval = TimeSpan.FromMilliseconds(1); // larger than zero to ensure we never get zero back
for (int i = 0; i < maxAttempts; i++)
{
Assert.IsTrue(retryPolicy.ShouldRetry(i, (int)HttpStatusCode.InternalServerError, e, out retryInterval, context), string.Format("Attempt: {0}", i));
Assert.IsTrue(retryInterval >= previousRetryInterval, string.Format("Retry Interval: {0}, Previous Retry Interval: {1}, Attempt: {2}", retryInterval, previousRetryInterval, i));
previousRetryInterval = retryInterval;
}
Assert.IsFalse(retryPolicy.ShouldRetry(maxAttempts, (int)HttpStatusCode.InternalServerError, e, out retryInterval, context));
}