验证Azure表插入操作的好方法是什么? [英] What's a good method for verifying an Azure Table Insert operation?

查看:39
本文介绍了验证Azure表插入操作的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直依赖 TableResult.HttpStatusCode = 204 作为 Insert 操作的积极结果,具体如下:

So far I've been relying on the TableResult.HttpStatusCode = 204 as a positive result from an Insert operation as per:

TableResult.HttpStatusCode属性

如果该实体已经存在,则得到 200 ,如果成功插入操作,则得到 204 .我还尝试按照以下方式在 TableOperation 上使用 echoContent 参数:

I get a 200 if the entity exists already and a 204 on a successful insert operation. I've also tried using the echoContent parameter on the TableOperation as per:

TableOperation.Insert方法

在我的情况下, echoContent 不返回任何内容,所以我不确定自己做错了还是追逐独角兽.我不确定 echoContent 是只是发回您发送给它的数据,还是实际上回显了书面实体.

The echoContent doesn't return anything in my case so I'm not sure if I'm doing something wrong or if I'm chasing unicorns. I wasn't sure if the echoContent just sends back the data you sent to it or if it actually echo's back the written entity.

是否有更好的验证方法?我是不是过于依赖Azure Table存储的REST API,还是我们仅此而已?

Is there a better way to validate? Am I relying on the REST api of Azure Table storage too much or is this all we have?

推荐答案

我实际上首先是在进行读取操作,然后评估结果以查看代码中早先是否存在重复项.无需先检查就执行的插入操作确实确实返回了 409 ,但是在调试了由于在现有实体之上插入而引起的异常之后,才发现此错误.

I was actually doing a read operation first, then evaluating the result to see if a duplicate exists earlier on in my code. The insert operation performed without checking first did indeed return a 409, however this was found after debugging an exception caused by inserting over top of an existing entity.

我的第一部分代码不那么频繁地进行活动,而更重要的是,我要检查一个重复的实体,这就是为什么我先读取,然后检查,然后是插入的原因.

My first bit of code was a much less frequent activity, and more important that I check for a duplicate entity which is why I did the read first, then the check, then an insert.

我后面的代码来自一个API,该API吸收了大量数据,我不想通过查找&在插入之前进行比较.

My latter code was from an API which is taking in a considerable amount of data and I didn't want to burden the transaction with a lookup & compare before the insert.

为解决此问题,我设置了一个try/catch块来监视 409 的特定异常类型:

To solve for this, I set a try/catch block to watch for the specific exception type of 409:

catch (Microsoft.WindowsAzure.Storage.StorageException e) when (e.RequestInformation.HttpStatusCode == 409)
        {
            TableResult tableResult = new TableResult();
            tableResult.HttpStatusCode = e.RequestInformation.HttpStatusCode;
            tableResult.Result = e.Message;
            return tableResult;
        }

由于我的方法返回的是 TableResult 类型,因此我必须格式化新的 tableResult 才能发送回调用方.使用调用方上的switch语句,我能够处理各种情况(成功/异常).

Since my method was returning a TableResult type, I had to format a new tableResult to send back to the caller. Using a switch statement on the caller I was able to handle the various scenarios (success/exception).

这篇关于验证Azure表插入操作的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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