c# – Azure DocumentDB读取文档资源未找到
发布时间:2020-12-15 04:20:07 所属栏目:百科 来源:网络整理
导读:我正在构建一个.Net控制台应用程序来读取DocumentDB中的信息.控制台应用程序具有来自EventHub的数据,并在进入云时插入/更新最新数据. 我正在尝试从DocumentDB读取单个文档,我可以在请求文档之前确认文档存在. if (DocumentDBRepositoryDocumentDBItem.DoesIt
我正在构建一个.Net控制台应用程序来读取DocumentDB中的信息.控制台应用程序具有来自EventHub的数据,并在进入云时插入/更新最新数据.
我正在尝试从DocumentDB读取单个文档,我可以在请求文档之前确认文档存在. if (DocumentDBRepository<DocumentDBItem>.DoesItemExist(name)) { device = await DocumentDBRepository<DocumentDBItem>.GetItemAsync(name); } 我使用了Microsoft的this教程,建立了一个用于访问DocumentDB记录的存储库,并成功地使用了几乎所有的方法.我可以更新/删除/查询数据库,但我无法读取单数项. 首先,它抛出了一个请求PartitionKey的异常.所以我修改了方法,将DB使用的PartitionKey添加到请求中.一旦我添加了PartitionKey,它就会抛出另一个异常,并显示一条消息“找不到资源” public static async Task<T> GetItemAsync(string id) { try { RequestOptions options = new RequestOptions(); options.PartitionKey = new PartitionKey("DeviceId"); Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId,CollectionId,id),options); return (T)(dynamic)document; } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { return null; } else { throw; } } } 我已经修改了我的调用以使用“GetItemsAsyc”并获得IEnumerable的文档并获得列表中的第一项,但是为什么我可以使用教程中的所有其他方法但这一点仍然有效是没有意义的抛出异常说“资源未找到”. 我得到的例外情况: "Message: {"Errors":["Resource Not Found"]}rnActivityId: e317ae66-6500-476c-b70e-c986c4cbf1d9,Request URI: /apps/e842e452-2347-4a8e-8588-2f5f8b4803ad/services/2c490552-a24d-4a9d-a786-992c07356545/partitions/0281cfdd-0c60-499f-be4a-289723a7dbf9/replicas/131336364114731886s" 解决方法
如
the documentation所示,您需要提供分区键的值,而不是存储分区键的字段的名称.因此,您需要将设备Id作为参数添加到方法中,并将其值传递给PartitionKey构造函数.
从示例: // Read document. Needs the partition key and the ID to be specified Document result = await client.ReadDocumentAsync( UriFactory.CreateDocumentUri("db","coll","XMS-001-FE24C"),new RequestOptions { PartitionKey = new PartitionKey("XMS-0001") }); 所以对于你的代码: public static async Task<T> GetItemAsync(string id,string deviceId) { try { RequestOptions options = new RequestOptions(); options.PartitionKey = new PartitionKey(deviceId); Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId,options); return (T)(dynamic)document; } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { return null; } else { throw; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c# – LdapConnection与PrincipalContext
- ruby-on-rails – 无法跟踪rails中的会话
- C#导入导出EXCEL文件的代码实例
- AJAX——核心XMLHttpRequest对象
- 在使用托管ODP.NET时,如何从C#查询LDAP以解析Oracle TNS主机
- Postgresql standby(备机只读)环境搭建
- NSJSONSerialization(category)的一个扩展类
- Swift语言IOS8开发战记25 网络通信Get和Post方式
- Ext.Ajax.request的waitMsg
- 故事板 – 未知的segue关系:具有NSCollectionView的原型
推荐文章
站长推荐
热点阅读