public static string DownloadTextAPM(CloudFile file, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null) { using (MemoryStream stream = new MemoryStream()) { using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = file.BeginDownloadToStream(stream, accessCondition, options, operationContext, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadToStream(result); return(encoding.GetString(stream.ToArray())); } } }
public void FileDisableContentMD5ValidationTestAPM() { FileRequestOptions optionsWithNoMD5 = new FileRequestOptions() { DisableContentMD5Validation = true, StoreFileContentMD5 = true, }; FileRequestOptions optionsWithMD5 = new FileRequestOptions() { DisableContentMD5Validation = false, StoreFileContentMD5 = true, }; CloudFileShare share = GetRandomShareReference(); try { share.Create(); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result; CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2"); using (Stream stream = new MemoryStream()) { file.UploadFromStream(stream, null, optionsWithMD5); } using (Stream stream = new MemoryStream()) { result = file.BeginDownloadToStream(stream, null, optionsWithMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadToStream(result); result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadToStream(result); file.Properties.ContentMD5 = "MDAwMDAwMDA="; file.SetProperties(); result = file.BeginDownloadToStream(stream, null, optionsWithMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); TestHelper.ExpectedException( () => file.EndDownloadToStream(result), "Downloading a file with invalid MD5 should fail", HttpStatusCode.OK); result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadToStream(result); } } } finally { share.DeleteIfExists(); } }
public void CloudFileDownloadToStreamAPMRetry() { byte[] buffer = GetRandomBuffer(1 * 1024 * 1024); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); using (MemoryStream originalFile = new MemoryStream(buffer)) { using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { ICancellableAsyncResult result = file.BeginUploadFromStream(originalFile, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndUploadFromStream(result); using (MemoryStream downloadedFile = new MemoryStream()) { Exception manglerEx = null; using (HttpMangler proxy = new HttpMangler(false, new[] { TamperBehaviors.TamperNRequestsIf( session => ThreadPool.QueueUserWorkItem(state => { Thread.Sleep(1000); try { session.Abort(); } catch (Exception e) { manglerEx = e; } }), 2, AzureStorageSelectors.FileTraffic().IfHostNameContains(share.ServiceClient.Credentials.AccountName)) })) { OperationContext operationContext = new OperationContext(); result = file.BeginDownloadToStream(downloadedFile, null, null, operationContext, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadToStream(result); TestHelper.AssertStreamsAreEqual(originalFile, downloadedFile); } if (manglerEx != null) { throw manglerEx; } } } } } finally { share.DeleteIfExists(); } }