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

Shr*_*kar 4 c# exception azure 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表存储返回400错误请求

  • 尝试将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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

创建并插入存储表代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

TableStorage类的对象包含: tableStorage obj

可能是什么问题?我是全新的天蓝色储物桌。请帮忙。提前致谢。

Gau*_*tri 5

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

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