0

Trying to do insert to CosmosDb using DaprClient. Below is code

[HttpPost("saveData")]
public async Task<ActionResult<string>> SaveData([FromBody] InputMessage message, [FromServices] DaprClient daprClient)
{

    Dictionary<string, string> data = new Dictionary<string, string> { { "partitionKey", "ServiceName" } };

    if (message is null)
        return new BadRequestObjectResult(new { error = $"The client is null" });

    var state = await daprClient.GetStateEntryAsync<TestModel>(_cosmosStoreName, "ServiceName", metadata: data);


    if (state.Value != null)
    {
        return new BadRequestObjectResult(new { error = $"The client with id {message.id} already exists" });
    };

    state.Value = new TestModel
    {
        ServiceName = "ServiceName"
    };


    await state.SaveAsync();
    return "Good";
}

PartitionKey is ServiceName. When I run dapr cli, and make request, I get below error

{\"code\":\"BadRequest\",\"message\":\"Message: {\\\"Errors\\\":[\\\"PartitionKey extracted from document doesn't match the one specified in the header. Learn more: https:\\\\/\\\\/aka.ms\\\\/CosmosDB\\\\/sql\\\\/errors\\\\/wrong-pk-value\\\"]}\\r\\nActivityId: 6ca949f9-da2a-4fe9-a346-a67196817670

When i trace for log with above activityid in CosmosDb, i see in AzureDiagnostics, for Category PartitionKeyRUConsumption, partitionKey_s as "fanglava-coyote||ServiceName"

So my guess is DAPR is giving out partitionKey like that. What am i doing wrong here? Can you please guide how it need to be implemented?

metadata parameter was added later on, when call by just passing key gave same result.

3
  • Created a new container with partitionkey as "partitionKey", and that seem to work. But when running with dapr cli, id is captured as "ocelotdull-throat||72b93714-f87e-4189-a877-351fd4380cf0". Why is it prefixing name? Also adding metadata to change partitionkey doesnt seem to work Commented May 1, 2024 at 14:18
  • id is not the same thing as partitionKey (unless you literally choose /id as your partition key). And you should be specifying a value of a document's partition key when writing, not the field name. As for id value: Seems like Dapr is generating an id for you (which every document needs). Please be sure to edit your question with additional detail - it shouldn't be in comments. Commented May 1, 2024 at 14:57
  • @DavidMakogon Ya obviously id is not partitionkey. why /id, also partitionKey is getting prefixed with Dapr Id, like "fanglava-coyote"? Also please be sure to comment if you know on dapr integration Commented May 2, 2024 at 14:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.