Azure的表存储返回400错误请求 [英] Azure table storage returns 400 Bad Request

查看:148
本文介绍了Azure的表存储返回400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调试模式下跑了这一点,我附上图片与异常的详细信息。我怎么能知道哪里出了问题?我试图在一个表中的数据插图。不能蔚蓝给我更多的细节?

观测:存储是在Windows Azure上没有我的机器上。该表被创建,但插入数据时出现此错误

  //从连接字符串存储帐户。
Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(\"DefaultEndpointsProtocol=https;AccountName=***;AccountKey=***\");//创建表的客户端。
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();//创建表,如果它不存在。
CloudTable表= tableClient.GetTableReference(EmployeeOnlineHistory);
table.CreateIfNotExists();

和这里是插入code:

 公共静态无效SetStatus(员工E,布尔值)
{
    尝试
    {
        //从连接字符串存储帐户。
        Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(\"DefaultEndpointsProtocol=https;AccountName=###;AccountKey=###\");        //创建表的客户端。
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();        //创建CloudTable对象重新presents人表。
        CloudTable表= tableClient.GetTableReference(EmployeeOnlineHistory);        //创建一个新客户的实体。        如果(价值==真)
        {
            EmployeeOnlineHistory empHistory =新EmployeeOnlineHistory(e.Id);
            empHistory.IsOnline =真;
            empHistory.OnlineTimestamp = DateTime.Now;
            TableOperation insertOperation = TableOperation.Insert(empHistory);
            table.Execute(insertOperation);
        }
        其他
        {
            TableQuery< EmployeeOnlineHistory>查询=新TableQuery< EmployeeOnlineHistory>()
                。凡(TableQuery.GenerateFilterCondition(PartitionKey,QueryComparisons.Equal,e.Id.ToString()));
            EmployeeOnlineHistory实体= table.ExecuteQuery(查询)。取(1).FirstOrDefault();            如果((实体= NULL)及!及(entity.IsOnline))
            {
                entity.IsOnline = FALSE;
                entity.OfflineTimestamp = DateTime.Now;
                entity.OnlineTime =(entity.OfflineTimestamp - entity.OnlineTimestamp);
                TableOperation updateOperation = TableOperation.Replace(实体);
                table.Execute(updateOperation);
            }
            其他
            {
                EmployeeOnlineHistory empHistory =新EmployeeOnlineHistory(e.Id);
                empHistory.IsOnline = FALSE;
                empHistory.OfflineTimestamp = DateTime.Now;
                TableOperation insertOperation = TableOperation.Insert(empHistory);
                table.Execute(insertOperation);
            }
        }
    }
    赶上(异常前)
    {
        // VAR细节=新System.IO.StreamReader(((Microsoft.WindowsAzure.Storage.StorageException)ex)..Response.GetResponseStream()).ReadToEnd();
        LogFile.Error(EmployeeOnlineHistory.setStatus,前);
    }
}


解决方案

400错误意味着有一些错误的属性之一的值。找出一种方法是跟踪通过的Fiddler请求/响应和看到实际的数据被发送到Windows Azure存储。

瞎猜,我通过采取快速浏览一下你的code,在你的模型,你有一些日期/时间类型的属性(OfflineTimestamp,OnlineTimestamp)假设,并指出,在某些情况下其中之一是与作为默认值初始化 DateTime.MinValue 。请注意,允许的日期/时间属性类型在最小值为1601年1月1日(UTC)在Windows Azure中的[http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx].请看看事实并非如此。如果是这样的话,那么你可以让他们空类型的字段,以便它们不会使用默认值填充。

看一看下面尤哈Palomäki的答案,以及...有时存在在他建议(RequestInformation.ExtendedErrorInformation.ErrorMessage)

除外稍微有用信息

I ran this in debug mode, and I attach an image with the details of the exception. How can I know what went wrong? I was trying to inset data in a table. Can't azure give me more details?

Obs: The storage is on Windows Azure not on my machine. The tables were created, but I get this error when inserting data

// Retrieve the storage account from the connection string.
Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=***;AccountKey=***");

// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Create the table if it doesn't exist.
CloudTable table = tableClient.GetTableReference("EmployeeOnlineHistory");
table.CreateIfNotExists();

and here is the insert code:

public static void SetStatus(Employee e, bool value)
{
    try
    {
        // Retrieve the storage account from the connection string.
        Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=###;AccountKey=###");

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Create the CloudTable object that represents the "people" table.
        CloudTable table = tableClient.GetTableReference("EmployeeOnlineHistory");

        // Create a new customer entity.

        if (value == true)
        {
            EmployeeOnlineHistory empHistory = new EmployeeOnlineHistory(e.Id);
            empHistory.IsOnline = true;
            empHistory.OnlineTimestamp = DateTime.Now;
            TableOperation insertOperation = TableOperation.Insert(empHistory);
            table.Execute(insertOperation);
        }
        else
        {
            TableQuery<EmployeeOnlineHistory> query = new TableQuery<EmployeeOnlineHistory>()
                .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, e.Id.ToString()));
            EmployeeOnlineHistory entity = table.ExecuteQuery(query).Take(1).FirstOrDefault();

            if ((entity!=null)&&(entity.IsOnline))
            {
                entity.IsOnline = false;
                entity.OfflineTimestamp = DateTime.Now;
                entity.OnlineTime = (entity.OfflineTimestamp - entity.OnlineTimestamp);
                TableOperation updateOperation = TableOperation.Replace(entity);
                table.Execute(updateOperation);
            }
            else
            {
                EmployeeOnlineHistory empHistory = new EmployeeOnlineHistory(e.Id);
                empHistory.IsOnline = false;
                empHistory.OfflineTimestamp = DateTime.Now;
                TableOperation insertOperation = TableOperation.Insert(empHistory);
                table.Execute(insertOperation);
            }
        }
    }
    catch (Exception ex)
    {
        //var details = new System.IO.StreamReader(((Microsoft.WindowsAzure.Storage.StorageException)ex)..Response.GetResponseStream()).ReadToEnd();
        LogFile.Error("EmployeeOnlineHistory.setStatus",ex);
    }
}

解决方案

400 Error means there's something wrong with the value of one of your properties. One way to find out is to trace the request/response through Fiddler and see the actual data being sent to Windows Azure Storage.

Taking a wild guess, I'm assuming by taking a quick glance at your code that in your model you have some Date/Time type properties (OfflineTimestamp, OnlineTimestamp) and observed that in certain scenarios one of them is initialized with the default value which is "DateTime.MinValue". Please note that the minimum value allowed for a Date/Time type attribute is Jan 1, 1601 (UTC) in Windows Azure[http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx]. Please see if that's not the case. If that's the case, then you could make them nullable type fields so that they don't get populated with the default values.

Have a look at Juha Palomäki's answer below as well... there sometimes is a slightly more useful message in the exception where he suggests (RequestInformation.ExtendedErrorInformation.ErrorMessage)

这篇关于Azure的表存储返回400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆