public void FileUseTransactionalMD5GetTestAPM() { FileRequestOptions optionsWithNoMD5 = new FileRequestOptions() { UseTransactionalMD5 = false, }; FileRequestOptions optionsWithMD5 = new FileRequestOptions() { UseTransactionalMD5 = true, }; byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); MD5 hasher = MD5.Create(); string md5 = Convert.ToBase64String(hasher.ComputeHash(buffer)); string lastCheckMD5 = null; int checkCount = 0; OperationContext opContextWithMD5Check = new OperationContext(); opContextWithMD5Check.ResponseReceived += (_, args) => { if (long.Parse(HttpResponseParsers.GetContentLength(args.Response)) >= buffer.Length) { lastCheckMD5 = HttpResponseParsers.GetContentMD5(args.Response); checkCount++; } }; CloudFileShare share = GetRandomShareReference(); try { share.Create(); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result; CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2"); using (Stream fileStream = file.OpenWrite(buffer.Length * 2)) { fileStream.Write(buffer, 0, buffer.Length); fileStream.Write(buffer, 0, buffer.Length); } checkCount = 0; using (Stream stream = new MemoryStream()) { result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadRangeToStream(result); Assert.IsNull(lastCheckMD5); result = file.BeginDownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); StorageException storageEx = TestHelper.ExpectedException <StorageException>( () => file.EndDownloadRangeToStream(result), "File will not have MD5 set by default; with UseTransactional, download should fail"); result = file.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadRangeToStream(result); Assert.IsNull(lastCheckMD5); result = file.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadRangeToStream(result); Assert.AreEqual(md5, lastCheckMD5); result = file.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadRangeToStream(result); Assert.IsNull(lastCheckMD5); result = file.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); storageEx = TestHelper.ExpectedException <StorageException>( () => file.EndDownloadRangeToStream(result), "Downloading more than 4MB with transactional MD5 should not be supported"); Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException)); result = file.BeginOpenRead(null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream fileStream = file.EndOpenRead(result)) { fileStream.CopyTo(stream); Assert.IsNotNull(lastCheckMD5); } result = file.BeginOpenRead(null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream fileStream = file.EndOpenRead(result)) { fileStream.CopyTo(stream); Assert.IsNull(lastCheckMD5); } } Assert.AreEqual(9, checkCount); } } finally { share.DeleteIfExists(); } }