dapr / dotnet-sdk

Dapr SDK for .NET
Apache License 2.0
1.12k stars 340 forks source link

How to use ExecuteStateTransactionAsync() #1266

Closed koumdev closed 7 months ago

koumdev commented 8 months ago

Ask your question here

Hello,

I'm trying to use ExecuteStateTransactionAsync() with Azure Cosmos DB state store, but it does not work. I keep getting "error while executing state transaction: transaction failed due to operation 0 which failed with status code 400" exception.

I tried exactly what is in the Dapr documentation in .NET here:

var random = new Random();
int orderId = random.Next(1, 1000);
var requests = new List<StateTransactionRequest>()
{
    new("order_1", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert)
};
await _daprClient.ExecuteStateTransactionAsync(STORE_NAME, requests, cancellationToken: cancellation);

I also tried it with PowerShell:

Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '{"operations": [{"operation":"upsert", "request": {"key": "order_1", "value": "250"}}, {"operation":"upsert", "request": {"key": "order_2", "value": "450"}}]}' -Uri 'http://localhost:3500/v1.0/state/statestorecosmos/transaction'

The documentation says that Azure Cosmos DB state store supports transactional operations.

What am I doing wrong?

Thanks!

philliphoff commented 7 months ago

@koumdev Did the PoserShell approach work, or did it return the same 400 error? If the latter, then that would point to a possible issue with the runtime or component rather than the .NET SDK. In that case, I might ask the question in the dapr/components-contrib repo which would be more familiar with Azure Cosmos DB.

koumdev commented 7 months ago

@philliphoff thank you for your reply and sorry for posting in the wrong place. Yes, I get the same 400 error with PowerShell. I posted in the components repo here.

philliphoff commented 7 months ago

Closing this in favor of the components-contrib issue.