示例#1
0
        public void FileUseTransactionalMD5PutTestAPM()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                UseTransactionalMD5 = false,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                UseTransactionalMD5 = true,
            };

            byte[] buffer = GetRandomBuffer(1024);
            MD5    hasher = MD5.Create();
            string md5    = Convert.ToBase64String(hasher.ComputeHash(buffer));

            string           lastCheckMD5          = null;
            int              checkCount            = 0;
            OperationContext opContextWithMD5Check = new OperationContext();

            opContextWithMD5Check.SendingRequest += (_, args) =>
            {
                if (HttpRequestParsers.GetContentLength(args.Request) >= buffer.Length)
                {
                    lastCheckMD5 = HttpRequestParsers.GetContentMD5(args.Request);
                    checkCount++;
                }
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    CloudFile    file = share.GetRootDirectoryReference().GetFileReference("file2");
                    file.Create(buffer.Length);
                    checkCount = 0;
                    using (Stream fileData = new MemoryStream(buffer))
                    {
                        result = file.BeginWriteRange(fileData, 0, null, null, optionsWithNoMD5, opContextWithMD5Check,
                                                      ar => waitHandle.Set(),
                                                      null);
                        waitHandle.WaitOne();
                        file.EndWriteRange(result);
                        Assert.IsNull(lastCheckMD5);

                        fileData.Seek(0, SeekOrigin.Begin);
                        result = file.BeginWriteRange(fileData, 0, null, null, optionsWithMD5, opContextWithMD5Check,
                                                      ar => waitHandle.Set(),
                                                      null);
                        waitHandle.WaitOne();
                        file.EndWriteRange(result);
                        Assert.AreEqual(md5, lastCheckMD5);

                        fileData.Seek(0, SeekOrigin.Begin);
                        result = file.BeginWriteRange(fileData, 0, md5, null, optionsWithNoMD5, opContextWithMD5Check,
                                                      ar => waitHandle.Set(),
                                                      null);
                        waitHandle.WaitOne();
                        file.EndWriteRange(result);
                        Assert.AreEqual(md5, lastCheckMD5);
                    }
                    Assert.AreEqual(3, checkCount);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }