+ public static Task<byte[]> UploadDataTaskAsync(this WebClient webClient, Uri address, string method, byte[] data, CancellationToken cancellationToken, IProgress<UploadProgressChangedEventArgs> progress)
+ {
+ var tcs = new TaskCompletionSource<byte[]>(address);
+ if (cancellationToken.IsCancellationRequested)
+ {
+ tcs.TrySetCanceled();
+ }
+ else
+ {
+ CancellationTokenRegistration ctr = cancellationToken.Register(webClient.CancelAsync);
+ UploadDataCompletedEventHandler completedHandler = null;
+ UploadProgressChangedEventHandler progressHandler = null;
+ if (progress != null)
+ progressHandler = (s, e) => PithosEAPCommon.HandleProgress(tcs, e, () => e, progress);
+ completedHandler =(sender, e) =>PithosEAPCommon.HandleCompletion(tcs, true, e,() => e.Result,() =>
+ {
+ ctr.Dispose();
+ webClient.UploadDataCompleted -= completedHandler;
+ webClient.UploadProgressChanged -= progressHandler;
+ });
+ webClient.UploadDataCompleted += completedHandler;
+ webClient.UploadProgressChanged += progressHandler;
+ try
+ {
+ webClient.UploadDataAsync(address, method, data, tcs);
+ if (cancellationToken.IsCancellationRequested)
+ webClient.CancelAsync();
+ }
+ catch
+ {
+ webClient.UploadDataCompleted -= completedHandler;
+ webClient.UploadProgressChanged -= progressHandler;
+ throw;
+ }
+ }
+ return tcs.Task;
+ }