dotnet-sdk icon indicating copy to clipboard operation
dotnet-sdk copied to clipboard

Is it possible to store state value as an object instead of a serialized string while persisting state?

Open rpenha opened this issue 3 years ago • 3 comments

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!

rpenha avatar Nov 19 '21 23:11 rpenha

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): image 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.

newstrider avatar Feb 22 '22 21:02 newstrider

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.

rovangju avatar Apr 19 '22 07:04 rovangju

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" }
});

johanbenschop avatar Nov 24 '23 18:11 johanbenschop