Azure 存储 table returns 异常 400 错误请求

Azure storage table returns exception 400 Bad Request

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

检查调试器后:

StorageException.RequestInformation.ExtendedErrorInformation.ErrorMessage

显示 OutOfRangeInput 错误

"One of the request inputs is out of range.\nRequestId:862fdbae-6002-000c-1e7c-ef9373000000\nTime:2019-04-10T09:06:05.9840359Z"

我得到了这个线程的帮助 Azure table 存储 returns 400 错误请求

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

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

创建并插入存储Table代码:

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

Table存储class的对象包含:

可能是什么问题?我是全新的 Azure 存储 table。 请帮忙。 提前致谢。

我认为您的 class 继承 TableEntity 时可能缺少 default/parameter-less 构造函数。当从 TableStorage 接收到对象时,非常需要无参数构造函数来反序列化对象。

参考

可能更正的代码:

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

问题出在TXNDATE属性的值上。如果您查看您分享的图片,您发送的值是 1/1/01(即 DateTime.MinValue),而允许的最小值是 1/1/1601.

TXNDATE 传递正确的值后,您应该不会看到此错误。