Пример #1
0
        /// <summary>
        /// Gets the error details from an XML-formatted error stream.
        /// </summary>
        /// <param name="inputStream">The input stream.</param>
        /// <returns>The error details.</returns>
        public static async Task <StorageExtendedErrorInformation> ReadFromStreamAsync(Stream inputStream)
        {
            CommonUtility.AssertNotNull("inputStream", inputStream);

            if (inputStream.CanSeek && inputStream.Length < 1)
            {
                return(null);
            }

            StorageExtendedErrorInformation extendedErrorInfo = new StorageExtendedErrorInformation();

            try
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.Async = true;

                using (XmlReader reader = XmlReader.Create(inputStream, settings))
                {
                    await reader.ReadAsync();

                    extendedErrorInfo.ReadXml(reader);
                }

                return(extendedErrorInfo);
            }
            catch (XmlException)
            {
                // If there is a parsing error we cannot return extended error information
                return(null);
            }
        }
        /// <summary>
        /// Gets the error details from an XML-formatted error stream.
        /// </summary>
        /// <param name="inputStream">The input stream.</param>
        /// <returns>The error details.</returns>
        public static StorageExtendedErrorInformation ReadFromStream(Stream inputStream)
        {
            CommonUtility.AssertNotNull("inputStream", inputStream);

            if (inputStream.CanSeek && inputStream.Length < 1)
            {
                return null;
            }
            
            StorageExtendedErrorInformation extendedErrorInfo = new StorageExtendedErrorInformation();
            try
            {               
                using (XmlReader reader = XmlReader.Create(inputStream))
                {
                    reader.Read();
                    extendedErrorInfo.ReadXml(reader);
                }

                return extendedErrorInfo;
            }
            catch (XmlException)
            {
                // If there is a parsing error we cannot return extended error information
                return null;
            }
        }
        public void ExtendedErrorInfoVerifyXmlWithAdditionalDetails()
        {
            Uri                baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint);
            CloudBlobClient    client         = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials);
            CloudBlobContainer container      = client.GetContainerReference(Guid.NewGuid().ToString("N"));

            byte[] buffer     = TestBase.GetRandomBuffer(4 * 1024 * 1024);
            MD5    md5        = MD5.Create();
            string contentMD5 = Convert.ToBase64String(md5.ComputeHash(buffer));

            try
            {
                container.Create();
                CloudBlockBlob blob   = container.GetBlockBlobReference("blob1");
                List <string>  blocks = new List <string>();
                for (int i = 0; i < 2; i++)
                {
                    blocks.Add(Convert.ToBase64String(Guid.NewGuid().ToByteArray()));
                }

                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    blob.PutBlock(blocks[0], memoryStream, contentMD5);

                    int offset = buffer.Length - 1024;
                    memoryStream.Seek(offset, SeekOrigin.Begin);
                    StorageException e = TestHelper.ExpectedException <StorageException>(
                        () => blob.PutBlock(blocks[1], memoryStream, contentMD5),
                        "Invalid MD5 should fail with mismatch");

                    Assert.IsNotNull(e.RequestInformation.ExtendedErrorInformation);

                    StorageExtendedErrorInformation retrErrorInfo = new StorageExtendedErrorInformation();
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    StringBuilder sb = new StringBuilder();
                    using (XmlWriter writer = XmlWriter.Create(sb, settings))
                    {
                        e.RequestInformation.ExtendedErrorInformation.WriteXml(writer);
                    }

                    using (XmlReader reader = XmlReader.Create(new StringReader(sb.ToString())))
                    {
                        retrErrorInfo.ReadXml(reader);
                    }

                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, retrErrorInfo.ErrorCode);
                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorMessage, retrErrorInfo.ErrorMessage);
                    Assert.AreNotEqual(0, retrErrorInfo.AdditionalDetails.Count);
                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.AdditionalDetails.Count, retrErrorInfo.AdditionalDetails.Count);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Пример #4
0
        static StorageExtendedErrorInformation ReadFromStream(Stream inputStream)
        {
            StorageExtendedErrorInformation extendedErrorInfo = new StorageExtendedErrorInformation();

            try
            {
                using (XmlReader reader = XmlReader.Create(inputStream))
                {
                    reader.Read();
                    extendedErrorInfo.ReadXml(reader);
                }

                return(extendedErrorInfo);
            }
            catch (XmlException)
            {
                // If there is a parsing error we cannot return extended error information
                return(null);
            }
        }
        public void ExtendedErrorInfoVerifyXml()
        {
            Uri                baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint);
            CloudBlobClient    client         = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials);
            CloudBlobContainer container      = client.GetContainerReference(Guid.NewGuid().ToString("N"));

            try
            {
                StorageException e = TestHelper.ExpectedException <StorageException>(
                    () => container.GetPermissions(),
                    "Try to get permissions on a non-existent container");

                Assert.IsNotNull(e.RequestInformation.ExtendedErrorInformation);

                StorageExtendedErrorInformation retrErrorInfo = new StorageExtendedErrorInformation();
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                StringBuilder sb = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(sb, settings))
                {
                    e.RequestInformation.ExtendedErrorInformation.WriteXml(writer);
                }

                using (XmlReader reader = XmlReader.Create(new StringReader(sb.ToString())))
                {
                    retrErrorInfo.ReadXml(reader);
                }

                Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, retrErrorInfo.ErrorCode);
                Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorMessage, retrErrorInfo.ErrorMessage);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        static StorageExtendedErrorInformation ReadFromStream(Stream inputStream)
        {
            CommonUtility.AssertNotNull("inputStream", inputStream);

            if (inputStream.CanSeek && inputStream.Length < 1)
            {
                return null;
            }
            
            StorageExtendedErrorInformation extendedErrorInfo = new StorageExtendedErrorInformation();
            try
            {               
                using (XmlReader reader = XmlReader.Create(inputStream))
                {
                    reader.Read();
                    extendedErrorInfo.ReadXml(reader);
                }

                return extendedErrorInfo;
            }
            catch (XmlException)
            {
                // If there is a parsing error we cannot return extended error information
                return null;
            }
        }
        public void ExtendedErrorInfoVerifyXml()
        {
            Uri baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint);
            CloudBlobClient client = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials);
            CloudBlobContainer container = client.GetContainerReference(Guid.NewGuid().ToString("N"));
            
            try
            {
                StorageException e = TestHelper.ExpectedException<StorageException>(
                    () => container.GetPermissions(),
                    "Try to get permissions on a non-existent container");

                Assert.IsNotNull(e.RequestInformation.ExtendedErrorInformation);

                StorageExtendedErrorInformation retrErrorInfo = new StorageExtendedErrorInformation();
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                StringBuilder sb = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(sb, settings))
                {
                    e.RequestInformation.ExtendedErrorInformation.WriteXml(writer);
                }

                using (XmlReader reader = XmlReader.Create(new StringReader(sb.ToString())))
                {
                    retrErrorInfo.ReadXml(reader);
                }

                Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, retrErrorInfo.ErrorCode);
                Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorMessage, retrErrorInfo.ErrorMessage);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void ExtendedErrorInfoVerifyXmlWithAdditionalDetails()
        {
            Uri baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint);
            CloudBlobClient client = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials);
            CloudBlobContainer container = client.GetContainerReference(Guid.NewGuid().ToString("N"));

            byte[] buffer = TestBase.GetRandomBuffer(4 * 1024 * 1024);
            MD5 md5 = MD5.Create();
            string contentMD5 = Convert.ToBase64String(md5.ComputeHash(buffer));

            try
            {
                container.Create();
                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                List<string> blocks = new List<string>();
                for (int i = 0; i < 2; i++)
                {
                    blocks.Add(Convert.ToBase64String(Guid.NewGuid().ToByteArray()));
                }

                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    blob.PutBlock(blocks[0], memoryStream, contentMD5);

                    int offset = buffer.Length - 1024;
                    memoryStream.Seek(offset, SeekOrigin.Begin);
                    StorageException e = TestHelper.ExpectedException<StorageException>(
                        () => blob.PutBlock(blocks[1], memoryStream, contentMD5),
                        "Invalid MD5 should fail with mismatch");

                    Assert.IsNotNull(e.RequestInformation.ExtendedErrorInformation);

                    StorageExtendedErrorInformation retrErrorInfo = new StorageExtendedErrorInformation();
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    StringBuilder sb = new StringBuilder();
                    using (XmlWriter writer = XmlWriter.Create(sb, settings))
                    {
                        e.RequestInformation.ExtendedErrorInformation.WriteXml(writer);
                    }

                    using (XmlReader reader = XmlReader.Create(new StringReader(sb.ToString())))
                    {
                        retrErrorInfo.ReadXml(reader);
                    }

                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, retrErrorInfo.ErrorCode);
                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorMessage, retrErrorInfo.ErrorMessage);
                    Assert.AreNotEqual(0, retrErrorInfo.AdditionalDetails.Count);
                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.AdditionalDetails.Count, retrErrorInfo.AdditionalDetails.Count);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
 static StorageExtendedErrorInformation ReadFromStream(Stream inputStream)
        {
            StorageExtendedErrorInformation extendedErrorInfo = new StorageExtendedErrorInformation();
            try
            {
                using (XmlReader reader = XmlReader.Create(inputStream))
                {
                    reader.Read();
                    extendedErrorInfo.ReadXml(reader);
                }

                return extendedErrorInfo;
            }
            catch (XmlException)
            {
                // If there is a parsing error we cannot return extended error information
                return null;
            }
        }