[英]“Resource Not Found” error on deleting document from documentdb
[英]Azure DocumentDB Read Document Resource Not Found
我正在構建一個 .Net 控制台應用程序來讀取 DocumentDB 中的信息。 控制台應用程序具有來自 EventHub 的數據,並在它進入雲時插入/更新最近的數據。
我正在嘗試從 DocumentDB 讀取單個文檔,並且我可以在請求文檔之前確認該文檔存在。
if (DocumentDBRepository<DocumentDBItem>.DoesItemExist(name))
{
device = await DocumentDBRepository<DocumentDBItem>.GetItemAsync(name);
}
我用這個教程從微軟關於建立一個存儲庫訪問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\"]}\r\nActivityId: 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"
如文檔中所示,您需要提供分區鍵的值,而不是存儲分區鍵的字段的名稱。 因此,您需要將設備 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, CollectionId, id), options);
return (T)(dynamic)document;
}
catch (DocumentClientException e)
{
if (e.StatusCode == HttpStatusCode.NotFound)
{
return null;
}
else
{
throw;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.