-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathBulkStateExample.cs
54 lines (40 loc) · 2.06 KB
/
BulkStateExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Dapr.Client;
namespace Samples.Client
{
public class BulkStateExample : Example
{
private static readonly string firstKey = "testKey1";
private static readonly string secondKey = "testKey2";
private static readonly string firstEtag = "123";
private static readonly string secondEtag = "456";
private static readonly string storeName = "statestore";
public override string DisplayName => "Using the State Store";
public override async Task RunAsync(CancellationToken cancellationToken)
{
using var client = new DaprClientBuilder().Build();
var state1 = new Widget() { Size = "small", Color = "yellow", };
var state2 = new Widget() { Size = "big", Color = "green", };
var stateItem1 = new SaveStateItem<Widget>(firstKey, state1, firstEtag);
var stateItem2 = new SaveStateItem<Widget>(secondKey, state2, secondEtag);
await client.SaveBulkStateAsync(storeName, new List<SaveStateItem<Widget>>() { stateItem1, stateItem2});
Console.WriteLine("Saved 2 States!");
await Task.Delay(2000);
IReadOnlyList<BulkStateItem> states = await client.GetBulkStateAsync(storeName,
new List<string>(){firstKey, secondKey}, null);
Console.WriteLine($"Got {states.Count} States: ");
var deleteBulkStateItem1 = new BulkDeleteStateItem(states[0].Key, states[0].ETag);
var deleteBulkStateItem2 = new BulkDeleteStateItem(states[1].Key, states[1].ETag);
await client.DeleteBulkStateAsync(storeName, new List<BulkDeleteStateItem>() { deleteBulkStateItem1, deleteBulkStateItem2 });
Console.WriteLine("Deleted States!");
}
private class Widget
{
public string? Size { get; set; }
public string? Color { get; set; }
}
}
}