本文整理汇总了C#中System.Net.Http.HttpClient.GetAsyncCancellationSafe方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.GetAsyncCancellationSafe方法的具体用法?C# HttpClient.GetAsyncCancellationSafe怎么用?C# HttpClient.GetAsyncCancellationSafe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.GetAsyncCancellationSafe方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DownloadContentsAsync
/// <summary>
/// Downloads the contents of the item to the local filesystem. This operation is only valid for files.
/// </summary>
/// <param name="destinationPath">Path to the destination file that will contain the contents of this item.</param>
/// <param name="feedbackChannel">Allows you to receive feedback about the operation while it is running.</param>
/// <param name="cancellationToken">Allows you to cancel the operation.</param>
public async Task DownloadContentsAsync(string destinationPath, IFeedbackChannel feedbackChannel = null, CancellationToken cancellationToken = default(CancellationToken))
{
Argument.ValidateIsNotNullOrWhitespace(destinationPath, "destinationPath");
if (Type != ItemType.File)
throw new InvalidOperationException("You can only download files.");
PatternHelper.LogMethodCall("DownloadContentsAsync", feedbackChannel, cancellationToken);
PatternHelper.EnsureFeedbackChannel(ref feedbackChannel);
using (var file = File.Create(destinationPath))
{
// If this file does not have any contents, there is nothing to download :)
if (!HasContents)
return;
using (await _client.AcquireLock(feedbackChannel, cancellationToken))
{
feedbackChannel.Status = "Requesting download URL";
var result = await _client.ExecuteCommandInternalAsync<GetDownloadLinkResult>(feedbackChannel, cancellationToken, new GetDownloadCommand
{
ItemID = ID
});
feedbackChannel.Status = "Preparing for download";
var itemKey = _client.DecryptItemKey(EncryptedKeys);
var dataKey = Algorithms.DeriveNodeDataKey(itemKey);
var nonce = itemKey.Skip(16).Take(8).ToArray();
var metaMac = itemKey.Skip(24).Take(8).ToArray();
feedbackChannel.Status = "Pre-allocating file";
file.SetLength(result.Size);
feedbackChannel.Status = "Downloading";
var chunkSizes = Algorithms.MeasureChunks(result.Size);
var chunkCount = chunkSizes.Length;
var chunkMacs = new byte[chunkCount][];
// Limit number of chunks in flight at the same time.
var concurrentDownloadSemaphore = new SemaphoreSlim(4);
// Only one file write operation can take place at a time.
var concurrentWriteSemaphore = new SemaphoreSlim(1);
// For progress calculations.
long completedBytes = 0;
CancellationTokenSource chunkDownloadsCancellationSource = new CancellationTokenSource();
// Get chunks in parallel.
List<Task> chunkDownloads = new List<Task>();
for (int i = 0; i < chunkCount; i++)
{
int chunkIndex = i;
long startOffset = chunkSizes.Take(i).Select(size => (long)size).Sum();
long endOffset = startOffset + chunkSizes[i];
// Each chunk is downloaded and processed by this separately.
chunkDownloads.Add(Task.Run(async delegate
{
var operationName = string.Format("Downloading chunk {0} of {1}", chunkIndex + 1, chunkSizes.Length);
using (var chunkFeedbackChannel = feedbackChannel.BeginSubOperation(operationName))
{
byte[] bytes = null;
using (await SemaphoreLock.TakeAsync(concurrentDownloadSemaphore))
{
await RetryHelper.ExecuteWithRetryAsync(async delegate
{
chunkDownloadsCancellationSource.Token.ThrowIfCancellationRequested();
chunkFeedbackChannel.Status = string.Format("Downloading {0} bytes", chunkSizes[chunkIndex]);
// Range is inclusive, so do -1 for the end offset.
var url = result.DownloadUrl + "/" + startOffset + "-" + (endOffset - 1);
HttpResponseMessage response;
using (var client = new HttpClient())
response = await client.GetAsyncCancellationSafe(url, chunkDownloadsCancellationSource.Token);
response.EnsureSuccessStatusCode();
bytes = await response.Content.ReadAsByteArrayAsync();
if (bytes.Length != chunkSizes[chunkIndex])
throw new MegaException(string.Format("Expected {0} bytes in chunk but got {1}.", chunkSizes[chunkIndex], bytes.Length));
}, ChunkDownloadRetryPolicy, chunkFeedbackChannel, chunkDownloadsCancellationSource.Token);
}
//.........这里部分代码省略.........