I've been trying figuring out how to setup a cosmos db client in .net web api, and not far enough.
I've noticed when using the cosmosdb ReadItemAsync function I always get 404 back with this response
With the following code:
public async Task<OkObjectResult> All()
{
var client = CosmosService.GetClient();
var container = client.GetDatabase("Development").GetContainer("Notes");
var result = await container.ReadItemAsync<CosmosNote>("1717482138", new PartitionKey("id"));
return Ok(result);
}
I have also tried using a different function. I have tried using ReadManyItemsAsync, which surprisingly work for some weird reason!
public async Task<OkObjectResult> All()
{
var client = CosmosService.GetClient();
var container = client.GetDatabase("Development").GetContainer("Notes");
List<(string, PartitionKey)> itemsToFind = new()
{
("1717482138", new PartitionKey("id"))
};
var data = await container.ReadManyItemsAsync<CosmosNote>(itemsToFind);
return Ok(data);
}
I don't get it why ReadItemAsync isn't working, can anyone help?
Thanks!
In the PartitionKey
you need to use the value and not the property name. If you change it to:
var partition = new PartitionKey("1717482138"); //<- value instead of property
var result = await container.ReadItemAsync<CosmosNote>("1717482138", partition);
It should work assuming you're using the /id
as your partition in your Cosmos container.