dotnet-sdk
dotnet-sdk copied to clipboard
Is it possible to store state value as an object instead of a serialized string while persisting state?
Hi!
I'm trying to save state in a MongoDB state store. When the state is saved using the .NET Dapr client (https://docs.dapr.io/developing-applications/sdks/dotnet/dotnet-client/#save--get-application-state), it stores the state value as a serialized string in the database.
However, if I save the same object using the Dapr HTTP Store API (https://docs.dapr.io/developing-applications/building-blocks/state-management/howto-get-save-state/#step-2-save-and-retrieve-a-single-state), the data is saved as an object.
Is it possible to use the .NET Dapr client to store the value as an object instead of a serialized string, like the Dapr HTTP Store API does?
Thanks!
I've realized this when using the Query API and obtaining weird results:
Here you can see two entities saved using the HTTP Store API (top two results) and two using the .NET Dapr client (bottom two):
The entities saved using the client are just serialized and saved as a string.
My tests with the Query API only work fine for the entries that used HTTP Store API.
For reference, I'm using a Docker container with MongoDB as a component for the state store and I'm not sure if the same problem happens when using Cosmos DB.
I noticed this with the go-sdk
as well; the direct HTTP store API stores as an object whereas thru the client it's turning into string.
I came across this behavior and discovered that when passed a content type for JSON to the metadata it will properly store the value as an object instead of a string.
_daprClient.SaveStateAsync("TheStateStoreName", key, value, metadata: new Dictionary<string, string>()
{
{ "contentType", "application/json" }
});