Azure存储表-更新条件不满足
当我尝试更新存储表上的实体时,会出现随机异常。我得到的例外是Azure存储表-更新条件不满足,azure,azure-storage-blobs,Azure,Azure Storage Blobs,当我尝试更新存储表上的实体时,会出现随机异常。我得到的例外是 System.Data.Services.Client.DataServiceRequestException: An error occurred while processing this request. ---> System.Data.Services.Client.DataServiceClientException: {"odata.error":{"code":"UpdateConditionNotSatisfi
System.Data.Services.Client.DataServiceRequestException: An error occurred while processing this request. ---> System.Data.Services.Client.DataServiceClientException: {"odata.error":{"code":"UpdateConditionNotSatisfied","message":{"lang":"en-US","value":"The update condition specified in the request was not satisfied.\nRequestId:2a205f10-0002-013b-028d-0bbec8000000\nTime:2015-10-20T23:17:16.5436755Z"}}} ---
我知道这可能是一个并发问题,但问题是没有其他进程访问该实体
有时我会遇到几十个这样的异常,我重新启动服务器,它又开始正常工作了
public static class StorageHelper
{
static TableServiceContext tableContext;
static CloudStorageAccount storageAccount;
static CloudTableClient CloudClient;
static StorageHelper()
{
storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudClient = storageAccount.CreateCloudTableClient();
tableContext = CloudClient.GetTableServiceContext();
tableContext.IgnoreResourceNotFoundException = true;
}
public static void Save(int myId,string newProperty,string myPartitionKey,string myRowKey){
var entity = (from j in tableContext.CreateQuery<MyEntity>("MyTable")
where j.PartitionKey == myId
select j).FirstOrDefault();
if (entity != null)
{
entity.MyProperty= myProperty;
tableContext.UpdateObject(entity);
tableContext.SaveChanges();
}
else
{
entity = new MyEntity();
entity.PartitionKey =MyPartitionKey;
entity.RowKey =MyRowKey;
entity.MyProperty= myProperty;
tableContext.AddObject("MyTable", entity);
tableContext.SaveChanges();
}
}
公共静态类StorageHelper
{
静态表服务上下文表上下文;
静态CloudStorageAccount-storageAccount;
静态CloudTableClient CloudClient;
静态StorageHelper()
{
storageAccount=CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(“StorageConnectionString”);
CloudClient=storageAccount.CreateCloudTableClient();
tableContext=CloudClient.GetTableServiceContext();
tableContext.IgnoreResourceNotFoundException=true;
}
公共静态void保存(int-myId、string-newProperty、string-myPartitionKey、string-myRowKey){
var entity=(来自tableContext.CreateQuery(“MyTable”)中的j)
其中j.PartitionKey==myId
选择j).FirstOrDefault();
如果(实体!=null)
{
entity.MyProperty=MyProperty;
tableContext.UpdateObject(实体);
tableContext.SaveChanges();
}
其他的
{
实体=新的MyEntity();
entity.PartitionKey=MyPartitionKey;
entity.RowKey=MyRowKey;
entity.MyProperty=MyProperty;
AddObject(“MyTable”,实体);
tableContext.SaveChanges();
}
}
>代码> 我建议您可以考虑从微软的企业库中使用<强>瞬态故障处理应用程序块< /强>,当您的应用程序遇到Azure中的这种短暂故障时,重试以尽量减少每次发生相同异常时服务器的重新启动。
更新实体时,设置ETag=“*”。
您修改的代码应该如下所示-
if (entity != null)
{
entity.MyProperty= "newProperty";
tableContext.UpdateObject(entity);
tableContext.SaveChanges();
}
您发布的代码使用了非常旧的表层,该表层现已过时。我们强烈建议您更新到存储库的较新版本并使用新的表层。有关详细信息,请参阅此。还请注意,如果您使用的是存储库的非常旧版本,这些表层最终将停止作为服务版本工作他们使用的将是不推荐使用的服务端
我们不建议客户像这里所做的那样重用TableServiceContext对象。它们包含各种跟踪,可能会导致性能问题以及其他不利影响。这些限制是我们推荐的部分原因(如上所述)移动到较新的表层。有关详细信息,请参阅
在表实体上,您必须发送指示etag的if match标头。如果您设置实体的etag值,库将为您设置此值。若要更新服务上实体的etag,请使用“*”
什么是tableContext?此代码段实际上没有显示对存储库的任何请求。好的,我用整个类更新了代码。Mayur,我没有看到您共享的代码中有任何修改。您是否缺少任何内容?谢谢Emily,我会检查您所说的所有内容并尽快尝试更新(它还不算太老,我在两年前就写了这段代码,这就是实现它的方法:)