Skip to content

Commit 3b9d892

Browse files
WhitWaldomsfussell
andauthored
Modernized the .NET examples for interacting with state in Dapr (#4622)
* Modernized the .NET examples for interacting with state in Dapr Signed-off-by: Whit Waldo <[email protected]> * Update daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md Co-authored-by: Mark Fussell <[email protected]> Signed-off-by: Whit Waldo <[email protected]> * Update daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md Co-authored-by: Mark Fussell <[email protected]> Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]> Co-authored-by: Mark Fussell <[email protected]>
1 parent c2119fd commit 3b9d892

File tree

1 file changed

+81
-113
lines changed

1 file changed

+81
-113
lines changed

daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md

Lines changed: 81 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -72,38 +72,27 @@ The following example shows how to save and retrieve a single key/value pair usi
7272

7373
```csharp
7474
75-
//dependencies
76-
using System;
77-
using System.Collections.Generic;
78-
using System.Net.Http;
79-
using System.Net.Http.Headers;
75+
using System.Text;
8076
using System.Threading.Tasks;
8177
using Dapr.Client;
82-
using Microsoft.AspNetCore.Mvc;
83-
using System.Threading;
84-
using System.Text.Json;
8578
86-
//code
87-
namespace EventService
79+
var builder = WebApplication.CreateBuilder(args);
80+
builder.Services.AddDaprClient();
81+
var app = builder.Build();
82+
83+
var random = new Random();
84+
//Resolve the DaprClient from its dependency injection registration
85+
using var client = app.Services.GetRequiredService<DaprClient>();
86+
87+
while(true)
8888
{
89-
class Program
90-
{
91-
static async Task Main(string[] args)
92-
{
93-
string DAPR_STORE_NAME = "statestore";
94-
while(true) {
95-
System.Threading.Thread.Sleep(5000);
96-
using var client = new DaprClientBuilder().Build();
97-
Random random = new Random();
98-
int orderId = random.Next(1,1000);
99-
//Using Dapr SDK to save and get state
100-
await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString());
101-
await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString());
102-
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, "order_1");
103-
Console.WriteLine("Result after get: " + result);
104-
}
105-
}
106-
}
89+
await Task.Delay(TimeSpan.FromSeconds(5));
90+
var orderId = random.Next(1,1000);
91+
//Using Dapr SDK to save and get state
92+
await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString());
93+
await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString());
94+
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, "order_1");
95+
Console.WriteLine($"Result after get: {result}");
10796
}
10897
```
10998

@@ -359,23 +348,20 @@ Below are code examples that leverage Dapr SDKs for deleting the state.
359348
{{% codetab %}}
360349

361350
```csharp
362-
//dependencies
363351
using Dapr.Client;
352+
using System.Threading.Tasks;
364353
365-
//code
366-
namespace EventService
367-
{
368-
class Program
369-
{
370-
static async Task Main(string[] args)
371-
{
372-
string DAPR_STORE_NAME = "statestore";
373-
//Using Dapr SDK to delete the state
374-
using var client = new DaprClientBuilder().Build();
375-
await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken);
376-
}
377-
}
378-
}
354+
const string DAPR_STORE_NAME = "statestore";
355+
356+
var builder = WebApplication.CreateBuilder(args);
357+
builder.Services.AddDaprClient();
358+
var app = builder.Build();
359+
360+
//Resolve the DaprClient from the dependency injection registration
361+
using var client = app.Services.GetRequiredService<DaprClient>();
362+
363+
//Use the DaprClient to delete the state
364+
await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken);
379365
```
380366

381367
To launch a Dapr sidecar for the above example application, run a command similar to the following:
@@ -540,22 +526,19 @@ Below are code examples that leverage Dapr SDKs for saving and retrieving multip
540526
{{% codetab %}}
541527

542528
```csharp
543-
//dependencies
544529
using Dapr.Client;
545-
//code
546-
namespace EventService
547-
{
548-
class Program
549-
{
550-
static async Task Main(string[] args)
551-
{
552-
string DAPR_STORE_NAME = "statestore";
553-
//Using Dapr SDK to retrieve multiple states
554-
using var client = new DaprClientBuilder().Build();
555-
IReadOnlyList<BulkStateItem> multipleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List<string> { "order_1", "order_2" }, parallelism: 1);
556-
}
557-
}
558-
}
530+
using System.Threading.Tasks;
531+
532+
const string DAPR_STORE_NAME = "statestore";
533+
534+
var builder = WebApplication.CreateBuilder(args);
535+
builder.Services.AddDaprClient();
536+
var app = builder.Build();
537+
538+
//Resolve the DaprClient from the dependency injection registration
539+
using var client = app.Services.GetRequiredService<DaprClient>();
540+
541+
IReadOnlyList<BulkStateItem> multipleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List<string> { "order_1", "order_2" }, parallelism: 1);
559542
```
560543

561544
To launch a Dapr sidecar for the above example application, run a command similar to the following:
@@ -567,28 +550,21 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
567550
The above example returns a `BulkStateItem` with the serialized format of the value you saved to state. If you prefer that the value be deserialized by the SDK across each of your bulk response items, you can instead use the following:
568551

569552
```csharp
570-
//dependencies
571553
using Dapr.Client;
572-
//code
573-
namespace EventService
574-
{
575-
class Program
576-
{
577-
static async Task Main(string[] args)
578-
{
579-
string DAPR_STORE_NAME = "statestore";
580-
//Using Dapr SDK to retrieve multiple states
581-
using var client = new DaprClientBuilder().Build();
582-
IReadOnlyList<BulkStateItem<Widget>> mulitpleStateResult = await client.GetBulkStateAsync<Widget>(DAPR_STORE_NAME, new List<string> { "widget_1", "widget_2" }, parallelism: 1);
583-
}
584-
}
554+
using System.Threading.Tasks;
585555
586-
class Widget
587-
{
588-
string Size { get; set; }
589-
string Color { get; set; }
590-
}
591-
}
556+
const string DAPR_STORE_NAME = "statestore";
557+
558+
var builder = WebApplication.CreateBuilder(args);
559+
builder.Serivces.AddDaprClient();
560+
var app = builder.Build();
561+
562+
//Resolve the DaprClient from the dependency injection registration
563+
using var client = app.Services.GetRequiredService<DaprClient>();
564+
565+
IReadOnlyList<BulkStateItem<Widget>> mulitpleStateResult = await client.GetBulkStateAsync<Widget>(DAPR_STORE_NAME, new List<string> { "widget_1", "widget_2" }, parallelism: 1);
566+
567+
record Widget(string Size, string Color);
592568
```
593569

594570
{{% /codetab %}}
@@ -791,44 +767,36 @@ Below are code examples that leverage Dapr SDKs for performing state transaction
791767
{{% codetab %}}
792768

793769
```csharp
794-
//dependencies
795-
using System;
796-
using System.Collections.Generic;
797-
using System.Net.Http;
798-
using System.Net.Http.Headers;
799-
using System.Threading.Tasks;
800770
using Dapr.Client;
801-
using Microsoft.AspNetCore.Mvc;
802-
using System.Threading;
803-
using System.Text.Json;
771+
using System.Threading.Tasks;
804772
805-
//code
806-
namespace EventService
773+
const string DAPR_STORE_NAME = "statestore";
774+
775+
var builder = WebApplication.CreateBuilder(args);
776+
builder.Serivces.AddDaprClient();
777+
var app = builder.Build();
778+
779+
//Resolve the DaprClient from the dependency injection registration
780+
using var client = app.Services.GetRequiredService<DaprClient>();
781+
782+
var random = new Random();
783+
784+
while (true)
807785
{
808-
class Program
809-
{
810-
static async Task Main(string[] args)
811-
{
812-
string DAPR_STORE_NAME = "statestore";
813-
while(true) {
814-
System.Threading.Thread.Sleep(5000);
815-
Random random = new Random();
816-
int orderId = random.Next(1,1000);
817-
using var client = new DaprClientBuilder().Build();
818-
var requests = new List<StateTransactionRequest>()
819-
{
820-
new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert),
821-
new StateTransactionRequest("order_2", null, StateOperationType.Delete)
822-
};
823-
CancellationTokenSource source = new CancellationTokenSource();
824-
CancellationToken cancellationToken = source.Token;
825-
//Using Dapr SDK to perform the state transactions
826-
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken);
827-
Console.WriteLine("Order requested: " + orderId);
828-
Console.WriteLine("Result: " + result);
829-
}
830-
}
831-
}
786+
await Task.Delay(TimeSpan.FromSeconds(5));
787+
var orderId = random.Next(1, 1000);
788+
var requests = new List<StateTransactionRequest>
789+
{
790+
new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert),
791+
new StateTransactionRequest("order_2", null, StateOperationType.Delete)
792+
};
793+
var cancellationTokenSource = new CancellationTokenSource();
794+
var cancellationToken = cancellationTokenSource.Token;
795+
796+
//Use the DaprClient to perform the state transactions
797+
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken);
798+
Console.WriteLine($"Order requested: {orderId}");
799+
Console.WriteLine($"Result: {result}");
832800
}
833801
```
834802

0 commit comments

Comments
 (0)