在azure表存储中获取错误的请求错误插入操作

时间:2017-12-27 14:01:34

标签: c# azure console-application azure-storage

在azure中执行控制台webjob时出现以下错误

Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T]
(RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 677
--- End of inner exception stack trace ---
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T]
(RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 604
at 
Microsoft.WindowsAzure.Storage.Table.TableOperation.Execute(CloudTableClient client, CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Table\TableOperation.cs:line 44
at Microsoft.WindowsAzure.Storage.Table.CloudTable.Execute(TableOperation operation, TableRequestOptions requestOptions, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Table\CloudTable.cs:line 52
at pAvg15MinInstantInputWebJob.Avg15MinInstantInputJob.AverageCalculator(DateTime runningDate) in F:\Projects\LogiProjects\ProcessEye\Source\Kent.ProcessEye\pAvg15MinInstantInputWebJob\Avg15MinInstantInputJob.cs:line 246
Request Information
RequestID:855533de-0002-0066-2819-7f27ee000000
RequestDate:Wed, 27 Dec 2017 13:51:21 GMT
StatusMessage:Bad Request
ErrorCode:InvalidInput
ErrorMessage:Bad Request - Error in query syntax.
RequestId:855533de-0002-0066-2819-7f27ee000000
Time:2017-12-27T13:51:22.1788418Z

但是在本地运行此应用程序它正在成功运行。

这是我的代码

string connectionString1 = ConfigurationManager.ConnectionStrings["AzureWebJobsDashboard"].ConnectionString;
CloudStorageAccount account1 = CloudStorageAccount.Parse(connectionString1);
CloudTableClient client1 = account1.CreateCloudTableClient();
CloudTable table2 = client1.GetTableReference(pAvg15MinInstantInput);
var row = new pAvg15MinInstantInputEntity();
row.PartitionKey = targetPartitionKey;
row.RowKey = rowDate.ToString();
row.avg = entity.average;
row.count = entity.count;
row.date = rowDate;
row.inputid = entity.inputid;
row.max = entity.max;
row.min = entity.min;
try
{
 TableOperation insertOperation = TableOperation.InsertOrReplace(row);
 table2.Execute(insertOperation);
 }
  catch (Exception ex)
  {
    Errorlogger(runningDate, "else ----->" + ex.ToString());
    return false;
   }

实体类如下

public class pAvg15MinInstantInputEntity : TableEntity
{
    public DateTime? date { get; set; }
    public string inputid { get; set; }
    public double? min { get; set; }
    public double? max { get; set; }
    public double? avg { get; set; }
    public long? count { get; set; }
}

更新

实体值如下

average - 244.1
count - 3
date - 2017-12-27 10:15:26
inputid - 36-i1
max-244.1
min - 244.0

,实体类如下

public class ResultModel
{
    public string inputid { get; set; }
    //public long hour { get; set; }
    //public long minute { get; set; }
    public double max { get; set; }
    public double min { get; set; }
    public DateTime date { get; set; }
    public double average { get; set; }
    public long count { get; set; }
}

解决此问题的任何解决方案?

1 个答案:

答案 0 :(得分:5)

由于Gaurav Mantri提到400错误通常由无效值引起。根据您提供的值,似乎所有值都是正确的除了 Rowkey 。如果我使用以下代码,我也可以重现它,因为它包含字符' /'。

RowKey = DateTime.Now.ToString(CultureInfo.InvariantCulture);

在您的情况下,您可以将RowKey值更改为修复值,例如" test" 以对其进行测试。我们还可以从Understanding the Table Service Data Model.

获取有关表格活动的更多信息
  

PartitionKey和RowKey 属性的值中不允许使用以下字符

     

正斜杠(/)字符

     

反斜杠()字符

     

数字符号(#)字符

     

问号(?)字符

     

控制字符从U + 0000到U + 001F,包括:

     

水平制表符(\ t)字符

     

换行符(\ n)字符

     

回车符(\ r)字符

     

控制字符从U + 007F到U + 009F

<强>更新

  

解决此问题的任何解决方案?

您可以使用以下代码

格式化 rowDate
row.RowKey = rowDate.ToString("yyyyMMddHHmmss")

row.RowKey = rowDate.AddMilliseconds(1).Ticks.ToString()

有关如何格式化数据时间的详细信息,请访问另一个SO thread