Azure表存储在插入时引发异常:(409)冲突 [英] Azure Table Storage throwing exception on Insert: (409) Conflict

查看:35
本文介绍了Azure表存储在插入时引发异常:(409)冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure Table Storage记录我的MVC应用程序中的访问者信息,但有时会引发以下异常:

I'm using Azure Table Storage to log visitor information from my MVC app, but it sometimes throws the following exception:

[WebException: The remote server returned an error: (409) Conflict.]
   System.Net.HttpWebRequest.GetResponse() +1399
   Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync(RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:677

[StorageException: The remote server returned an error: (409) Conflict.]
   Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync(RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:604
   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:44

似乎是在一段时间不活动后第一次访问该网站时发生的,然后当我单击刷新"时,页面加载并且此后的每次点击都很好.

It seems to happen when I first visit the website after a period of inactivity, then when I hit refresh, the page loads and every click from then on is fine.

这是导致异常的代码部分:

Here is the part of the code that's causing the exception:

  var visit = new TrackerVisitEntity(id, url, referer);
  var insertOperation = TableOperation.Insert(visit);
  _table.Execute(insertOperation);

更新

如评论和下面的两个答案中所述,问题在于有时页面会连续快速加载两次,而我使用的是GUID(对用户而言唯一)作为分区键,而当前日期时间为行键,因此这导致重复的实体并导致异常.

As identified in the comments AND both answers below, the problem is that sometimes the page is loaded twice in quick succession, and I'm using a GUID (unique to the user) as the partition key, and the current datetime as the row key, so this is causing duplicate entities and causing the exception.

尽管Amor的回答更为深入,但Dogu的简单解决方案是我使用的解决方案,因此我将其标记为正确.谢谢大家.

Although Amor's answer was more indepth, Dogu's simple solution was the one I used, so I marked his correct. Thanks everyone.

推荐答案

您可以尝试使用 InsertOrReplace 而不是 Insert 以避免409,如果出现以下情况,它将插入实体它不存在,如果存在则替换现有的.需要注意的是,它不会检查eTag,因此,如果存在一个具有相同分区键和行键的实体,它将无条件地覆盖它.

You could try InsertOrReplace instead of Insertto avoid 409, it is going to insert the entity if it does not exist and replace the existing one if it exists. The caveat with that is it does not check eTag s, so if there is an existing entity with the same partition key and row key it unconditionally overwrites it.

这篇关于Azure表存储在插入时引发异常:(409)冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆