Azure表存储返回400个不良请求 [英] Azure table storage returns 400 Bad Request

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

问题描述

我在调试模式下运行这个,我附加一个具有异常细节的图像。我怎么知道出了什么问题?我试图在表格中插入数据。不能azure给我更多的细节?



Obs:存储在Windows Azure上不在我的机器上。表已创建,但插入数据时收到此错误



  //从连接字符串中检索存储帐户。 
Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(DefaultEndpointsProtocol = https; AccountName = ***; AccountKey = ***);

//创建表客户端。
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

//如果表不存在,则创建表。
CloudTable table = tableClient.GetTableReference(EmployeeOnlineHistory);
table.CreateIfNotExists();

这里是插入代码:

  public static void SetStatus(Employee e,bool value)
{
try
{
//从连接字符串中检索存储帐户。
Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(DefaultEndpointsProtocol = https; AccountName = ###; AccountKey = ###);

//创建表客户端。
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

//创建表示人物表的CloudTable对象。
CloudTable table = tableClient.GetTableReference(EmployeeOnlineHistory);

//创建一个新的客户实体。

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)前).. Response.GetResponseStream())ReadToEnd的();
LogFile.Error(EmployeeOnlineHistory.setStatus,ex);
}
}


解决方案

400错误表示您的其中一个属性的值有问题。找出的一个方法是通过Fiddler跟踪请求/响应,并查看发送到Windows Azure Storage的实际数据。 Taking wild wild wild wild Taking,,,,and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and观察到在某些情况下,其中一个被初始化为默认值,即 DateTime.MinValue 。请注意,Windows Azure中允许使用日期/时间类型属性的最小值为1601(UTC) [http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx] 。请看是否不是这样。如果是这样,那么你可以使它们成为可空的类型字段,以便它们不被默认值填充。



看下面的JuhaPalomä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天全站免登陆