Azure DocumentDB读取文档资源未找到

And*_*ken 12 c# asp.net azure azure-eventhub azure-cosmosdb

我正在构建一个.Net控制台应用程序来读取DocumentDB中的信息.控制台应用程序具有来自EventHub的数据,并在进入云时插入/更新最新数据.

我正在尝试从DocumentDB读取单个文档,我可以在请求文档之前确认文档存在.

 if (DocumentDBRepository<DocumentDBItem>.DoesItemExist(name))
 {
     device = await DocumentDBRepository<DocumentDBItem>.GetItemAsync(name);
 }
Run Code Online (Sandbox Code Playgroud)

我使用Microsoft的这个教程来构建用于访问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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

PartitionKeyFromAzure

我已经修改了我的调用以使用"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"
Run Code Online (Sandbox Code Playgroud)

Mar*_*llo 15

文档中所示,您需要提供分区键的,而不是存储分区键的字段的名称.因此,您需要将设备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") });
Run Code Online (Sandbox Code Playgroud)

所以对于你的代码:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)