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

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

问题描述

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

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

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

我使用了

我已经修改了我的调用以使用GetItemsAsyc"并获取文档的 IEnumerable 并获取列表中的第一项,但是为什么我可以使用教程中的所有其他方法是没有意义的,但是这个继续抛出异常,说找不到资源".

我遇到的异常:

"Message: {"Errors":["Resource Not Found"]}
ActivityId: e317ae66-6500-476c-b70e-c986c4cbf1d9, 请求 URI:/apps/e842e452-2347-4a8e-8588-2f5f8b4803ad/services/2c490552-a24d-4a9d-a786-992c07356545/partitions/0281cfdd-0c60-499f-be4a-2897231616316317373163131313131313135373

解决方案

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

来自示例:

//读取文档.需要指定分区键和 ID文档结果 = 等待 client.ReadDocumentAsync(UriFactory.CreateDocumentUri("db", "coll", "XMS-001-FE24C"),new RequestOptions { PartitionKey = new PartitionKey("XMS-0001") });

所以对于您的代码:

public static async TaskGetItemAsync(string id, string deviceId){尝试{RequestOptions options = new RequestOptions();options.PartitionKey = new PartitionKey(deviceId);文档文档 = 等待 client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), options);返回(T)(动态)文档;}捕获 (DocumentClientException e){如果(e.StatusCode == HttpStatusCode.NotFound){返回空;}别的{扔;}}}

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.

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

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.

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

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

Exception I'm getting:

"Message: {"Errors":["Resource Not Found"]}
ActivityId: 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"

解决方案

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.

From the example:

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

So for your code:

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天全站免登陆