首页 文章

Azure表存储返回400 Bad Request

提问于
浏览
99

我在调试模式下运行它,并附上一个包含异常细节的图像 . 我怎么知道出了什么问题?我试图在表格中插入数据 . 天蓝不能给我更多细节?

Obs:存储在Windows Azure上而不在我的机器上 . 表已创建,但插入数据时出现此错误

enter image description here

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

15 回答

  • 3

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

    enter image description here

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

  • 126

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

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

  • 3

    我修好了我的案子并且工作正常

    我的案子:

    • 行键格式不正确(400) .

    • partitionkey和rowkey的组合不是唯一的(409) .

  • 0

    我收到400 Bad Request,因为我使用的是ZRS(Zone Redundant Storage),而且这种类型的存储无法使用Analytics . 我不知道我在使用Google Analytics .

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

  • 1

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

  • 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

    下划线工作正常 .

  • 3

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

    public class TableRunLogMessage:TableEntity
    {
          public string status { get; set; }
          public long logged { get; set; }
    
    
          public TableRunLogMessage() { }
    }
    
  • 0

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

    粗略猜测,我假设您快速浏览一下代码,在模型中您有一些日期/时间类型属性(OfflineTimestamp,OnlineTimestamp)并观察到在某些情况下其中一个用默认值初始化是“ DateTime.MinValue ” . 请注意Windows Azure中的 minimum value allowed for a Date/Time type attribute is Jan 1, 1601 (UTC) [http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx] . 请查看's not the case. If that'是否是这种情况,然后您可以将它们设置为可空类型字段,以便它们不会填充默认值 .

    看看下面的JuhaPalomäki的答案......在他建议的例外中,有时会有一些更有用的消息(RequestInformation.ExtendedErrorInformation.ErrorMessage)

  • 1

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

    签入调试器:StorageException.RequestInformation.ExtendedInformation

    enter image description here

  • 0

    就我而言,这是_1344330中的正斜线 .

    我还收到了'OutOfRangeInput - 其中一个请求输入超出了范围 . '尝试通过存储模拟器手动添加时出错 .

    键字段中不允许使用的字符PartitionKey和RowKey属性的值不允许使用以下字符:正斜杠(/)字符反斜杠(\)字符数字符号(#)字符问号(?)字符控制字符从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();
    }
    
  • 44

    我遇到了同样的问题,但我的理由是因为尺寸问题 . 在深入了解其他异常属性(RequestInformation.ExtendedErrorInformation)后,找到了原因:

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

  • 3

    在我的情况下:容器名称是大写字母 . 使用字符时有局限性 .
    enter image description here

  • 0

    有时这是因为 partitionKeyrowKeyNULL

    (我的情况就是这样)

  • 0

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

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

    因为ContainerName SessionMaterials (作为Pascal案例中的习惯写作和Camel案例:D)它导致400个错误请求 . 所以,我只需要做到 sessionmaterials . 它起作用了 .

    希望这对某人有所帮助 .

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

  • 103

    可以找到MS关于所有表服务错误代码的文档here

相关问题