找不到Azure DocumentDB读取文档资源 [英] Azure DocumentDB Read Document Resource Not Found

查看:74
本文介绍了找不到Azure DocumentDB读取文档资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a .Net Console application to read information in a DocumentDB. The console app has data coming from an EventHub and inserts/updates recent data as it comes into the cloud.

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

I am trying to read a singular document from DocumentDB and I can confirm that the Document Exists prior to requesting the Document.

 if (DocumentDBRepository<DocumentDBItem>.DoesItemExist(name))
 {
     device = await DocumentDBRepository<DocumentDBItem>.GetItemAsync(name);
 }

我使用了Microsoft的教程一个用于访问DocumentDB记录的存储库,并成功使用了几乎所有方法.我可以更新/删除/查询数据库,但无法读取单个项目.

I used this tutorial from Microsoft about building a Repository for accessing the DocumentDB records, and was successful at using almost all of the methods. I can update/delete/query the DB but I cannot read a singular Item.

首先,它引发了一个异常,要求提供PartitionKey.因此,我修改了将数据库使用的PartitionKey添加到请求的方法.一旦我添加了PartitionKey,它就会引发另一个异常,并显示一条消息:找不到资源"

First it was throwing an exception requesting a PartitionKey. So I modified the method to add the PartitionKey being used by the DB to the request. As soon as I added the PartitionKey it throws another exception with a message, "Resource Not Found"

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 of Documents并获得列表中的First项目,但是为什么我可以使用教程中的所有其他方法却没有任何意义,但是这一个人继续抛出异常,说找不到资源".

I've already modified my calls to use "GetItemsAsyc" and get an IEnumerable of Documents and get the First item in the list, but it doesn't make sense why I can use all of the other methods from the tutorial but this one continues to throw exceptions saying, "Resource Not Found".

我得到的异常:

"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"

推荐答案

文档,您需要提供分区键的 value ,而不是存储分区键的字段的名称.因此,您需要将设备ID作为参数添加到方法中,并将其值传递给PartitionKey构造函数.

As shown in the documentation, you need to provide the value of the partition key, not the name of the field which stores the partition key. Thus, you need to add the device Id as a parameter to your method and pass its value in to the PartitionKey constructor.

从示例中:

// 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;
        }
    }
}

这篇关于找不到Azure DocumentDB读取文档资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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