133

我在调试模式下运行它,并附上一张带有异常详细信息的图像。我怎么知道出了什么问题?我试图在表格中插入数据。azure不能给我更多细节吗?

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();

这是插入代码:

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);
    }
}
4

22 回答 22

174

400 错误表示您的某个属性的值有问题。找出答案的一种方法是通过 Fiddler 跟踪请求/响应并查看发送到 Windows Azure 存储的实际数据。

大胆猜测一下,我假设通过快速浏览您的代码,在您的模型中您有一些日期/时间类型属性(OfflineTimestamp、OnlineTimestamp)并观察到在某些情况下,其中一个使用默认值初始化是“ DateTime.MinValue ”。请注意,在 Windows Azure [http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx]中,日期/时间类型属性允许的最小值是 1601 年 1 月 1 日 (UTC)。请看看是不是这样。如果是这种情况,那么您可以将它们设为可为空的类型字段,这样它们就不会被填充默认值。

看看下面的 Juha Palomäki 的回答......有时在他建议的异常中会有一条更有用的消息 (RequestInformation.ExtendedErrorInformation.ErrorMessage)

于 2013-02-14T02:29:23.837 回答
144

StorageException 还包含有关错误的更详细信息。

签入调试器:StorageException.RequestInformation.ExtendedInformation

在此处输入图像描述

于 2014-08-20T08:57:39.567 回答
60

就我而言,它是RowKey中的正斜杠。

我还收到“OutOfRangeInput - 请求输入之一超出范围”。尝试通过存储模拟器手动添加时出错。

关键字段中不允许使用的字符

PartitionKeyRowKey属性的值中不允许使用以下字符 :

  • 正斜杠 ( / ) 字符
  • 反斜杠 ( \ ) 字符
  • 数字符号 ( # ) 字符
  • 问号 ( ? ) 字符
  • 从U+0000 到 U+001F的控制字符,包括:
    • 水平制表符 ( \t ) 字符
    • 换行符 ( \n ) 字符
    • 回车 ( \r ) 字符
    • U+007F 到 U+009F 的控制字符

http://msdn.microsoft.com/en-us/library/dd179338.aspx

我写了一个扩展方法来为我处理这个。

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();
}
于 2014-03-14T18:05:31.337 回答
6

我遇到了同样的问题,但在我的情况下,原因是尺寸。深挖附加异常属性(RequestInformation.ExtendedErrorInformation)后,发现原因:

ErrorCode : PropertyValueTooLarge ErrorMessage: 属性值超过了允许的最大大小 (64KB)。如果属性值为字符串,则为 UTF-16 编码,最大字符数应为 32K 或更少。

于 2016-06-10T20:21:28.340 回答
5

好吧,就我而言,我试图这样做:

CloudBlobContainer container = blobClient.GetContainerReference("SessionMaterials");
await container.CreateIfNotExistsAsync();

由于 ContainerName SessionMaterials(作为 Pascal Case 和 Camel Case 的习惯写作:D),它导致了 400 个错误请求。所以,我只需要成功sessionmaterials。它奏效了。

希望这对某人有所帮助。

PS:- 只需检查异常 http 响应或使用 fiddler 捕获请求和响应。

于 2018-05-24T08:10:34.643 回答
3

有时是因为你的partitionKey或是rowKeyNULL

(对我来说就是这种情况)

于 2016-06-07T13:07:58.437 回答
3

在我的情况下:容器名称是大写字母。使用字符时有限制。 在此处输入图像描述

于 2017-08-24T20:06:52.620 回答
1

可以在此处找到 MS 提供的有关所有表服务错误代码的文档

于 2017-06-12T09:35:56.760 回答
1

当实体未设置属性 DateTime (= DateTime.MinValue) 时,我收到 (400) Bad Request, StatusMessage:Bad Request, ErrorCode:OutOfRangeInput

于 2018-02-06T08:26:03.033 回答
1

我有同样的 BadRequest(400) 错误,最后我手动填写:

在此处输入图像描述

并为我工作。希望这可以帮助!

于 2018-06-15T07:26:20.890 回答
0

我也面临同样的问题。在我的情况下,未设置 PartitionKey 值,因此默认情况下 PartitionKey 值为 null,导致Object reference not set to an instance of an object.异常

检查您是否为 PartitionKey 或 RowKey 提供了适当的值,您可能会遇到此类问题。

于 2015-09-01T19:45:45.043 回答
0

我修好了我的案子,效果很好

我的案例:

  1. 行键格式不正确 (400)。
  2. partitionkey 和 rowkey 的组合不是唯一的 (409)。
于 2016-07-12T22:23:48.260 回答
0

我收到了 400 错误请求,因为我使用的是 ZRS(区域冗余存储),而 Analytics 不适用于这种类型的存储。我不知道我在使用 Analytics。

我删除了存储容器并重新创建为 GRS,现在它工作正常。

于 2016-11-06T06:07:28.707 回答
0

在我的例子中:我包含了带有连字符的标记名称的 blob 元数据。

var blob = container.GetBlockBlobReference(filename);
blob.Metadata.Add("added-by", Environment.UserName);
//.. other metadata
blob.UploadFromStream(filestream);

破折号"added-by"是问题所在,后来 RTFM 告诉我标签名称必须符合 C# 标识符约定。

参考:https ://docs.microsoft.com/en-us/azure/storage/blobs/storage-properties-metadata

下划线工作正常。

于 2018-07-29T21:24:10.923 回答
0

就我而言,我不应该在我的实体类中添加 PartitionKey 和 Rowkey。它应该来自基类。下面就可以了。

public class TableRunLogMessage:TableEntity
{
      public string status { get; set; }
      public long logged { get; set; }


      public TableRunLogMessage() { }
}
于 2018-11-28T19:31:30.007 回答
0

如果你在使用 NodeJS 并偶然发现这篇文章,你会发现你的错误对象中没有得到那么可爱的详细信息;您可以使用代理来获取这些详细信息。但是,由于这里没有人提到如何使用代理。

NodeJS 最简单的方法是设置两个环境变量:

NODE_TLS_REJECT_UNAUTHORIZED=0
This disables SSL checks so you can intercept your own SSL requests. This leaves you open to Man-in-The-Middle attacks and should NEVER make it to production, and I wouldn't even leave it in development for long. However, it will allow you to intercept the HTTP Requests.

HTTP_PROXY=http://127.0.0.1:8888
This sets node to utilize a proxy listening on your localhost at port 8888. Port 8888 is the default for Fiddler. Many other proxies default to 8080.

如果你真的在使用 C#,就像这篇文章的作者正在做的那样;您可以简单地安装 Fiddler 并将其设置为拦截。默认情况下,它应该拦截请求。您可能还需要信任 Fiddler 的证书,或者执行与 Node 的“NODE_TLS_REJECT_UNAUTHORIZED=0”等效的操作。

于 2019-01-08T18:05:36.733 回答
0

我从 Azure 存储帐户表 API 收到 400-BadRequest 响应。异常信息显示“正在访问的帐户不支持http。”。我认为当在存储帐户配置中启用“需要安全传输”时,我们必须在连接字符串中使用 https,如下图所示。在此处输入图像描述

于 2019-04-12T23:43:49.213 回答
0

在我创建“TableBotDataStore”类(MS bot 框架)的新实例的情况下,我们将“tableName”参数与“master-bot”之类的连字符一起传递,而 TableBotDataStore 的表名只能包含字母和数字

于 2019-06-27T09:33:15.400 回答
0

我有同样的问题,该函数正在传递containerNameKeyas 字符串。下面是给出错误的代码

container = blobClient.GetContainerReference(containerNameKey) 

我把它改成

container = blobClient.GetContainerReference(ConfigurationManager.AppSettings(containerNameKey).ToString()) 

有效

于 2019-08-20T20:09:37.610 回答
0

当我尝试将太长的值放入字符串字段时,我得到了 400-BadRequest 响应。

“一个 UTF-16 编码的值。字符串值的大小可能高达 64 KiB。请注意,支持的最大字符数约为 32 K 或更少。”

https://docs.microsoft.com/en-us/rest/api/storageservices/understanding-the-table-service-data-model

于 2020-12-31T10:40:29.073 回答
0

对我来说,我使用了带有破折号的表名示例:tablename:"table-1234",但是没有破折号的 tablename:"table1234" 有效。

于 2021-01-20T10:35:06.887 回答
0

对我来说,我尝试使用包含“#”字符的 PartitionKey/RowKey 将实体插入 Azure 存储表。令人讨厌的是这是不允许的,因为在单表设计中通常使用“#”作为分隔符。

于 2021-02-26T14:43:50.150 回答