State Store

Add using statements for the state store related namespaces.

Implement IStateStore

Create a class that implements the IStateStore interface.

  1. internal sealed class MyStateStore : IStateStore
  2. {
  3. public Task DeleteAsync(StateStoreDeleteRequest request, CancellationToken cancellationToken = default)
  4. {
  5. // Delete the requested key from the state store...
  6. }
  7. public Task<StateStoreGetResponse?> GetAsync(StateStoreGetRequest request, CancellationToken cancellationToken = default)
  8. {
  9. // Get the requested key value from from the state store, else return null...
  10. public Task InitAsync(MetadataRequest request, CancellationToken cancellationToken = default)
  11. {
  12. // Called to initialize the component with its configured metadata...
  13. }
  14. public Task SetAsync(StateStoreSetRequest request, CancellationToken cancellationToken = default)
  15. // Set the requested key to the specified value in the state store...
  16. }
  17. }

Bulk state stores

State stores that intend to support bulk operations should implement the optional IBulkStateStore interface. Its methods mirror those of the base IStateStore interface, but include multiple requested values.

Note

The Dapr runtime will emulate bulk state store operations for state stores that do not implement IBulkStateStore by calling its operations individually.

  1. internal sealed class MyStateStore : IStateStore, IBulkStateStore
  2. {
  3. // ...
  4. public Task BulkDeleteAsync(StateStoreDeleteRequest[] requests, CancellationToken cancellationToken = default)
  5. {
  6. // Delete all of the requested values from the state store...
  7. public Task<StateStoreBulkStateItem[]> BulkGetAsync(StateStoreGetRequest[] requests, CancellationToken cancellationToken = default)
  8. {
  9. // Return the values of all of the requested values from the state store...
  10. }
  11. {
  12. // Set all of the values of the requested keys in the state store...
  13. }
  14. }

Queryable state stores

State stores that intend to support queries should implement the optional IQueryableStateStore interface. Its QueryAsync() method is passed details about the query, such as the filter(s), result limits and pagination, and sort order(s) of the results. The state store should use those details to generate a set of values to return as part of its response.

  1. internal sealed class MyStateStore : IStateStore, IQueryableStateStore
  2. {
  3. // ...
  4. public Task<StateStoreQueryResponse> QueryAsync(StateStoreQueryRequest request, CancellationToken cancellationToken = default)
  5. {
  6. // Generate and return results...
  7. }

The Dapr runtime has additional handling of certain error conditions resulting from some state store operations. State stores can indicate such conditions by throwing specific exceptions from its operation logic: