Azure存储表返回异常400错误请求

时间:2019-04-10 09:26:45

标签: c# azure exception azure-storage azure-table-storage

我正在尝试创建azure存储表,并使用c#将记录插入其中。 我已经成功创建了该表,但是在向其中插入记录时却给了400 Bad Request存储异常。

检入调试器后:

  

StorageException.RequestInformation.ExtendedErrorInformation.ErrorMessage

它显示OutOfRangeInput错误

  

“其中一个请求输入超出范围。\ nRequestId:862fdbae-6002-000c-1e7c-ef9373000000 \ n时间:2019-04-10T09:06:05.9840359Z”

我帮助了这个线程Azure table storage returns 400 Bad Request

  • 尝试将null传递给我的实体模型变量,以查看是否有记录超出范围
  • 尝试在RowKey中通过“测试”
  • 尝试通过PartitionKey中的“ 1” /“ test”
  • 尝试在RowKey中传递Guid.NewGuid()。ToString()或ToAzureKeyString(Guid.NewGuid()。ToString())
  • 尝试在时间戳中传递DateTimeOffset.Now

它仍然给我同样的错误。 这是我的代码:

public void GetGPSFileData(Config objConfig, TraceWriter log)
{
    try
    {
        BindData objData = new BindData();
        string date = DateTime.Now.Date.ToString("ddMMyyyy");
        string storageTable = "TABLE" + date + objData.REPCODE;
        TableStorage tableStorage = new TableStorage(objData);
        CreateTableStorage(objConfig, storageTable, tableStorage, log);
    }
    catch (StorageException ex)
    {
        log.Info($"Storage Exception while reading GPS File Data from Azure Storage: " + ex.RequestInformation.ExtendedErrorInformation.ErrorMessage + DateTime.Now);
        throw ex;
    }
    catch (Exception ex)
    {
        log.Info($"Error while reading GPS File Data from Azure Storage: " + ex.Message + DateTime.Now);
        throw ex;
    }
}

TableStorage.cs

public class TableStorage: TableEntity
{
    public string CLIENTID { get; set; }
    public string REPCODE { get; set; }
    public int ENTRYNO { get; set; }
    public string DEVICEID { get; set; }
    public double LAT { get; set; }
    public double LNG { get; set; }
    public DateTime DATE_TIME { get; set; }

    public string PCODE { get; set; }
    public string PNAME { get; set; }
    public DateTime TXNDATE { get; set; }

    public TableStorage(BindData objData)
    {
        PartitionKey = objData.CLIENTID;
        RowKey = ToAzureKeyString(Guid.NewGuid().ToString());
        Timestamp = DateTimeOffset.Now;

        CLIENTID = objData.CLIENTID;
        REPCODE = objData.REPCODE;
        ENTRYNO = objData.ENTRYNO;
        DEVICEID = objData.DEVICEID;
        LAT = objData.LAT;
        LNG = objData.LNG;
        DATE_TIME = objData.DATE_TIME;
        PCODE = objData.PCODE;
        PNAME = objData.PNAME;
        TXNDATE = objData.TXNDATE;
    }

    public string ToAzureKeyString(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();
    }
}

创建并插入存储表代码:

public void CreateTableStorage(Config objConfig, string tableName, TableStorage tableStorage, TraceWriter log)
{
    try
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(objConfig.TABLE_STORAGE_CONN_STRING);
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
        CloudTable table = tableClient.GetTableReference(tableName);
        table.CreateIfNotExists();

        TableOperation insert = TableOperation.Insert(tableStorage);
        table.Execute(insert);
    }
    catch(StorageException ex)
    {
        log.Info($"Storage Exception while inserting record into Table Storage: " + ex.RequestInformation.ExtendedErrorInformation.ErrorMessage + DateTime.Now);
        throw ex;
    }
    catch(Exception ex)
    {
        log.Info($"Error while inserting record into Table Storage: " + ex.Message + DateTime.Now);
        throw ex;
    }
}

TableStorage类的对象包含: tableStorage obj

可能是什么问题?我是全新的天蓝色储物桌。 请帮忙。 预先感谢。

2 个答案:

答案 0 :(得分:4)

我相信您的类继承TableEntity时可能缺少默认/无参数的构造函数。从TableStorage接收到该对象时,非常需要无参数的构造函数来反序列化该对象。

请参考this answer

可能的更正代码:

public class TableStorage: TableEntity
{
    public string CLIENTID { get; set; }
    public string REPCODE { get; set; }
    public int ENTRYNO { get; set; }
    public string DEVICEID { get; set; }
    public double LAT { get; set; }
    public double LNG { get; set; }
    public DateTime DATE_TIME { get; set; }

    public string PCODE { get; set; }
    public string PNAME { get; set; }
    public DateTime TXNDATE { get; set; }
    public TableStorage(){}//Added default constructor
    public TableStorage(BindData objData)
    {
        PartitionKey = objData.CLIENTID;
        RowKey = ToAzureKeyString(Guid.NewGuid().ToString());
        Timestamp = DateTimeOffset.Now;

        CLIENTID = objData.CLIENTID;
        REPCODE = objData.REPCODE;
        ENTRYNO = objData.ENTRYNO;
        DEVICEID = objData.DEVICEID;
        LAT = objData.LAT;
        LNG = objData.LNG;
        DATE_TIME = objData.DATE_TIME;
        PCODE = objData.PCODE;
        PNAME = objData.PNAME;
        TXNDATE = objData.TXNDATE;
    }

    public string ToAzureKeyString(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();
    }
}

答案 1 :(得分:4)

由于TXNDATE属性的值而出现问题。如果您查看共享的图片,则发送的值为1/1/01(即DateTime.MinValue),而允许的最小值为1/1/1601

一旦您为TXNDATE传递了正确的值,就不会看到此错误。