我正在尝试使用C#创建Azure存储表并向其中插入记录。我已经成功创建了该表,但在向其中插入记录时,它会出现400 Bad Request存储异常。
在调试器中进行检查后:
在调试器中进行检查后:
StorageException.RequestInformation.ExtendedErrorInformation.ErrorMessage
它显示OutOfRangeInput错误
"请求输入之一超出范围。\nRequestId:862fdbae-6002-000c-1e7c-ef9373000000\nTime:2019-04-10T09:06:05.9840359Z"
我参考了这个线程 Azure表存储返回400 Bad Request
并且
- 尝试将null传递到我的实体模型变量中,以查看是否有任何记录超出范围
- 尝试在RowKey中传递"test"
- 尝试在PartitionKey中传递"1"/"test"
- 尝试在RowKey中传递Guid.NewGuid().ToString()或ToAzureKeyString(Guid.NewGuid().ToString())
- 尝试在Timestamp中传递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;
}
}
问题可能是什么?我对Azure存储表完全不了解。 请帮忙。 提前致谢。