C# Azure 表存储返回 400 Bad Request
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14859405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Azure table storage returns 400 Bad Request
提问by Ryan
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?
我在调试模式下运行它,并附加了一个包含异常详细信息的图像。我怎么知道出了什么问题?我试图在表格中插入数据。azure 不能给我更多细节吗?
Obs: The storage is on Windows Azure not on my machine. The tables were created, but I get this error when inserting data
Obs:存储在 Windows Azure 上,而不是在我的机器上。表已创建,但在插入数据时出现此错误
// 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);
}
}
采纳答案by Gaurav Mantri-AIS
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.
400 错误表示您的属性之一的值有问题。找出问题的一种方法是通过 Fiddler 跟踪请求/响应并查看发送到 Windows Azure 存储的实际数据。
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.
大胆猜测一下,我假设通过快速浏览您的代码,您的模型中有一些日期/时间类型属性(OfflineTimestamp、OnlineTimestamp),并观察到在某些情况下其中一个使用默认值初始化是“ DateTime.MinValue”。请注意,在 Windows Azure [http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx] 中,日期/时间类型属性允许的最小值为 1601 年 1 月 1 日 (UTC)。请看看是不是这样。如果是这种情况,那么您可以使它们成为可为空类型的字段,这样它们就不会被默认值填充。
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)
看看下面的 Juha Palom?ki 的回答......有时在他建议的异常中会有一个更有用的消息(RequestInformation.ExtendedErrorInformation.ErrorMessage)
回答by Shawn
In my case it was a forward slash in the RowKey.
就我而言,它是RowKey 中的正斜杠。
I also received an 'OutOfRangeInput - One of the request inputs is out of range.' error when trying to add manually through the storage emulator.
我还收到了“OutOfRangeInput - 其中一个请求输入超出范围”。尝试通过存储模拟器手动添加时出错。
Characters Disallowed in Key Fields
The following characters are not allowed in values for the PartitionKeyand RowKeyproperties:
- The forward slash (/) character
- The backslash (\) character
- The number sign (#) character
- The question mark (?) character
- Control characters from U+0000 to U+001F, including:
- The horizontal tab (\t) character
- The linefeed (\n) character
- The carriage return (\r) character
- Control characters from U+007F to U+009F
关键字段中不允许使用的字符
PartitionKey和RowKey属性的值中不允许使用以下字符 :
- 正斜杠 ( /) 字符
- 反斜杠 ( \) 字符
- 数字符号 ( #) 字符
- 问号 ( ?) 字符
- 控制字符从U+0000 到 U+001F,包括:
- 水平制表符 ( \t) 字符
- 换行符 ( \n)
- 回车 ( \r) 字符
- 控制字符从U+007F 到 U+009F
http://msdn.microsoft.com/en-us/library/dd179338.aspx
http://msdn.microsoft.com/en-us/library/dd179338.aspx
I wrote an extension method to handle this for me.
我写了一个扩展方法来为我处理这个问题。
public static string ToAzureKeyString(this string str)
{
var sb = new StringBuilder();
foreach (var c in str
.Where(c => c != '/'
&& c != '\'
&& c != '#'
&& c != '/'
&& c != '?'
&& !char.IsControl(c)))
sb.Append(c);
return sb.ToString();
}
回答by Juha Palom?ki
The StorageException contains also a little bit more detailed information about error.
StorageException 还包含更多关于错误的详细信息。
Check in debugger: StorageException.RequestInformation.ExtendedInformation
签入调试器:StorageException.RequestInformation.ExtendedInformation
回答by Dilip Nannaware
I also faced same kind of issue. In my case PartitionKey value was not set, so by default PartitionKey value was null, which resulted in Object reference not set to an instance of an object.
exception
我也面临同样的问题。在我的情况下 PartitionKey 值未设置,因此默认情况下 PartitionKey 值为 null,这导致Object reference not set to an instance of an object.
异常
Check if you are providing the appropriate values for PartitionKey or RowKey, you may face such problem.
检查您是否为 PartitionKey 或 RowKey 提供了适当的值,您可能会遇到这样的问题。
回答by Maroine Abdellah
Sometimes it's because your partitionKey
or rowKey
is NULL
有时是因为你的partitionKey
或者rowKey
是NULL
(it was the case for me)
(我就是这种情况)
回答by Nibras Manna
I faced the same issue but the reason in my case was due to size. After digging into the additional exception properties (RequestInformation.ExtendedErrorInformation), found the reason:
我遇到了同样的问题,但在我的情况下,原因是由于大小。在深入研究了额外的异常属性(RequestInformation.ExtendedErrorInformation)后,找到了原因:
ErrorCode : PropertyValueTooLarge ErrorMessage: The property value exceeds the maximum allowed size (64KB). If the property value is a string, it is UTF-16 encoded and the maximum number of characters should be 32K or less.
ErrorCode : PropertyValueTooLarge ErrorMessage: 属性值超过允许的最大大小 (64KB)。如果属性值为字符串,则为 UTF-16 编码,最大字符数应为 32K 或更少。
回答by Kurkula
I fixed my cases and it worked fine
我修好了我的箱子,效果很好
My cases:
我的情况:
- Row key is not in correct format (400).
- Combination of partitionkey and rowkey is not unique (409).
- 行键的格式不正确 (400)。
- partitionkey 和 rowkey 的组合不是唯一的 (409)。
回答by Aidan
I was getting a 400 Bad Request because I was using ZRS (Zone Redundant Storage), and Analytics isn't available for this type of storage. I wasn't aware I was using Analytics.
我收到了 400 错误请求,因为我使用的是 ZRS(区域冗余存储),而 Analytics 不适用于这种类型的存储。我不知道我在使用 Analytics。
I deleted the storage container and recreated as GRS and now it works fine.
我删除了存储容器并重新创建为 GRS,现在它工作正常。