当前位置: 首页 > 知识库问答 >
问题:

Azure存储表返回异常400错误请求[重复]

田权
2023-03-14

我正在尝试创建azure存储表,并使用c#向其中插入记录。我已经成功地创建了表,但是在向其中插入记录时,它给出了400错误请求存储异常

签入调试器后:

StorageException.RequestInformation.ExtendedErrorInformation.ErrorMessage

它显示超出范围输入错误

\n请求 Id:862fdbae-6002-000c-1e7c-ef9373000000\n时间:2019-04-10T09:06:05.9840359Z”

我得到了这个线程的帮助 Azure 表存储返回 400 错误请求

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

但它还是给了我同样的错误。这是我的代码:

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

表格存储.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;
    }
}

有什么问题吗?我是全新的蔚蓝收纳桌。请帮忙。提前感谢。

共有2个答案

松琦
2023-03-14

我相信继承TableEntity的类中可能缺少一个默认/无参数构造函数。当从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();
    }
}
史淳
2023-03-14

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

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

 类似资料:
  • 我在调试模式下运行它,并附加了一个包含异常详细信息的图像。我怎么知道出了什么问题?我试图在表中插入数据。azure不能告诉我更多细节吗? Obs:存储在Windows Azure上,而不是我的机器上。表已创建,但我在插入数据时出现此错误 下面是插入代码:

  • 问题内容: 这工作正常: 这将返回400 Bad Request(只是使用.ajax来支持错误处理的上述jQuery的重新格式)。 问题答案: 我认为您只需要再添加2个选项(和):

  • 我试图简单地使用Azure创建一个新的存储队列,但它一直崩溃而没有解释,创建表工作正常,这是相关代码: 行< code>healingQueue中的代码崩溃。CreateIfNotExists();以及< code>(400)错误请求的说明 表创建得很好,所以我假设存储帐户没有问题,知道我能做什么吗?

  • 我试图使用Gmail API将用户设置应用到Gmail帐户,但它不断返回错误400错误请求。 我可以看到错误代码在Gmail API控制台,它来自我的服务号,所以代码不可能是如此错误,但它让我发疯,只是不能找出什么是错误的。 如果有人能给我指出正确的方向,我会非常感激。

  • 问题内容: 我有这个应用程序,它可以在本地运行,并且在部署时可以使用.mdf SQL Express数据库文件(通常用于测试目的)。但是,当我将其更改为可与我们的SQL Server 2008一起使用时,该应用程序可以运行,但该服务无法运行。 例如,如果在页面后面的代码中,我有一个按钮可以向表中添加数据,例如: 我的web.config设置为在该服务器上使用模拟,并且一切运行良好。但是,对于我的服

  • 我试图对我写的rest api执行一个put请求,但它总是返回400-bug请求。 java.io.IO异常:服务器返回HTTP响应代码:400为URL:http://localhost:8000/api/v0/contacts/2sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源) 我可以用firefox rest客户端