@H_502_4@
我多年来一直在使用azure表存储,我不确定使用最新的
WindowsAzure.Storage库5.0.1版预览版(用于新的ASP.NET)的“正确”方法是什么5申请):
问题:
给定分区键和行键,删除行而不先检查是否存在,如果不存在则不会失败.
public async Task DeleteRowAsync(CloudTable table,string partition,string row) { var entity = new DynamicTableEntity(partition,row); entity.ETag = "*"; var op = TableOperation.Delete(entity); try { await table.ExecuteAsync(op); } catch (Exception ex) { var result = RequestResult.TranslateFromExceptionMessage(ex.Message); if (result == null || result.HttpStatusCode != 404) throw ex; } }
问题:
>异常本身向我指出了这个TranslateFromExceptionMessage方法……我找不到关于它的大量信息和WrappedStorageException(抛出的异常的类型).这是检查存储异常404错误的某种新的/首选方法吗?有谁知道现在所有的存储异常都会使用它,还是我需要编写代码来测试并找出它?
>存在StorageException类型的InnerException.据推测,我们使用StorageException.Request@R_640_404[email protected]的旧代码可以以相同的方式访问此内部异常.这是“OK”,还是以某种方式解析这些新的XML错误消息更好或更强大?
>对于这种情况,我应该考虑采用不同的方法吗?
解决方法
配置为使用Azure表存储时,在AspNet WebHooks项目中可以找到类似的方法.看一下Microsoft.Aspnet.WebHooks.custom.AzureStorage
StorageManager类.
我不确定这会在您已经找到的内容之上增加很多内容,但是它们会处理所有内容而不会抛出异常并始终返回状态代码,以便您可以根据需要对此做出反应.
这里的一个区别是它们将表和操作传递给多用途的ExecuteAsync方法,而不是专门用于删除,但这只是一个实现细节.
他们的例子中的相关代码:
public async Task<TableResult> ExecuteAsync(CloudTable table,TableOperation operation) { if (table == null) { throw new ArgumentNullException(nameof(table)); } if (operation == null) { throw new ArgumentNullException(nameof(operation)); } try { var result = await table.ExecuteAsync(operation); return result; } catch (Exception ex) { var errorMessage = GetStorageErrorMessage(ex); var statusCode = GetStorageStatusCode(ex); var message = string.Format(CultureInfo.CurrentCulture,AzureStorageResources.StorageManager_OperationFailed,statusCode,errorMessage); _logger.Error(message,ex); return new TableResult { HttpStatusCode = statusCode }; } } public string GetStorageErrorMessage(Exception ex) { if (ex is StorageException storageException && storageException.Request@R_640_4045@ion != null) { var status = storageException.Request@R_640_404[email protected] != null ? storageException.Request@R_640_404[email protected] + " " : string.Empty; var errorCode = storageException.Request@R_640_404[email protected]@R_640_4045@ion != null ? "(" + storageException.Request@R_640_404[email protected]@R_640_404[email protected] + ")" : string.Empty; return status + errorCode; } else if (ex != null) { return ex.Message; } return string.Empty; } public int GetStorageStatusCode(Exception ex) { return ex is StorageException se && se.Request@R_640_4045@ion != null ? se.Request@R_640_404[email protected] : 500; }