Azure DocumentDB read document resource unavailable

I am creating a .Net Console application to read information in DocumentDB. The console application has data coming from EventHub and inserts / updates the latest data when it gets to the cloud.

I am trying to read a unique document from DocumentDB, and I can confirm that the document exists before the document is requested.

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

I used this Microsoft tutorial on creating a repository to access DocumentDB records and have successfully used almost all of the methods. I can update / delete / query the database, but I cannot read the particular item.

First, he threw an exception requesting PartitionKey. Therefore, I modified the method to add the PartitionKey used by the database to the query. As soon as I added PartitionKey, it throws another exception with the 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; } } } 

PartitionKeyFromAzure

I already changed my calls to use "GetItemsAsyc" and get IEnumerable of Documents and get the first item in the list, but it doesn't make sense why I can use all the other methods from the tutorial, but it continues to throw exceptions saying: "The resource is not found. "

Exception I get:

 "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" 
+5
source share
2 answers

As shown in the documentation , you need to specify the value of the section key, and not the name of the field in which the section key is stored. Thus, you need to add the device identifier as a parameter to your method and pass its value to the PartitionKey constructor.

From an 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; } } } 
+6
source

Today I had an exact question.

When reading a document in a db document,
when docLink format (dbs / dbid ../ colls / collid ../ docs / docid ..)

Decision:
The client of the document chose "Resource not found" in two cases:
1. Collection ID is incorrect 2. This section has some problems. That is, either you gave the wrong KeyKey, or partitionKey was not required, but was specified.

+1
source

Source: https://habr.com/ru/post/1267136/


All Articles