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 <whit.waldo@innovian.net>

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

Co-authored-by: Mark Fussell <markfussell@gmail.com>
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

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

Co-authored-by: Mark Fussell <markfussell@gmail.com>
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

---------

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
Co-authored-by: Mark Fussell <markfussell@gmail.com>
This commit is contained in:
Whit Waldo 2025-05-16 20:17:07 -05:00 committed by GitHub
parent c2119fdd43
commit 3b9d89246e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 81 additions and 113 deletions

View File

@ -72,38 +72,27 @@ The following example shows how to save and retrieve a single key/value pair usi
```csharp ```csharp
//dependencies using System.Text;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapr.Client; using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Text.Json;
//code var builder = WebApplication.CreateBuilder(args);
namespace EventService builder.Services.AddDaprClient();
var app = builder.Build();
var random = new Random();
//Resolve the DaprClient from its dependency injection registration
using var client = app.Services.GetRequiredService<DaprClient>();
while(true)
{ {
class Program await Task.Delay(TimeSpan.FromSeconds(5));
{ var orderId = random.Next(1,1000);
static async Task Main(string[] args)
{
string DAPR_STORE_NAME = "statestore";
while(true) {
System.Threading.Thread.Sleep(5000);
using var client = new DaprClientBuilder().Build();
Random random = new Random();
int orderId = random.Next(1,1000);
//Using Dapr SDK to save and get state //Using Dapr SDK to save and get state
await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString()); await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString());
await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString()); await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString());
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, "order_1"); var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, "order_1");
Console.WriteLine("Result after get: " + result); Console.WriteLine($"Result after get: {result}");
}
}
}
} }
``` ```
@ -359,23 +348,20 @@ Below are code examples that leverage Dapr SDKs for deleting the state.
{{% codetab %}} {{% codetab %}}
```csharp ```csharp
//dependencies
using Dapr.Client; using Dapr.Client;
using System.Threading.Tasks;
//code const string DAPR_STORE_NAME = "statestore";
namespace EventService
{ var builder = WebApplication.CreateBuilder(args);
class Program builder.Services.AddDaprClient();
{ var app = builder.Build();
static async Task Main(string[] args)
{ //Resolve the DaprClient from the dependency injection registration
string DAPR_STORE_NAME = "statestore"; using var client = app.Services.GetRequiredService<DaprClient>();
//Using Dapr SDK to delete the state
using var client = new DaprClientBuilder().Build(); //Use the DaprClient to delete the state
await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken); await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken);
}
}
}
``` ```
To launch a Dapr sidecar for the above example application, run a command similar to the following: 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
{{% codetab %}} {{% codetab %}}
```csharp ```csharp
//dependencies
using Dapr.Client; using Dapr.Client;
//code using System.Threading.Tasks;
namespace EventService
{ const string DAPR_STORE_NAME = "statestore";
class Program
{ var builder = WebApplication.CreateBuilder(args);
static async Task Main(string[] args) builder.Services.AddDaprClient();
{ var app = builder.Build();
string DAPR_STORE_NAME = "statestore";
//Using Dapr SDK to retrieve multiple states //Resolve the DaprClient from the dependency injection registration
using var client = new DaprClientBuilder().Build(); using var client = app.Services.GetRequiredService<DaprClient>();
IReadOnlyList<BulkStateItem> multipleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List<string> { "order_1", "order_2" }, parallelism: 1);
} IReadOnlyList<BulkStateItem> multipleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List<string> { "order_1", "order_2" }, parallelism: 1);
}
}
``` ```
To launch a Dapr sidecar for the above example application, run a command similar to the following: 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
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: 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:
```csharp ```csharp
//dependencies
using Dapr.Client; using Dapr.Client;
//code using System.Threading.Tasks;
namespace EventService
{
class Program
{
static async Task Main(string[] args)
{
string DAPR_STORE_NAME = "statestore";
//Using Dapr SDK to retrieve multiple states
using var client = new DaprClientBuilder().Build();
IReadOnlyList<BulkStateItem<Widget>> mulitpleStateResult = await client.GetBulkStateAsync<Widget>(DAPR_STORE_NAME, new List<string> { "widget_1", "widget_2" }, parallelism: 1);
}
}
class Widget const string DAPR_STORE_NAME = "statestore";
{
string Size { get; set; } var builder = WebApplication.CreateBuilder(args);
string Color { get; set; } builder.Serivces.AddDaprClient();
} var app = builder.Build();
}
//Resolve the DaprClient from the dependency injection registration
using var client = app.Services.GetRequiredService<DaprClient>();
IReadOnlyList<BulkStateItem<Widget>> mulitpleStateResult = await client.GetBulkStateAsync<Widget>(DAPR_STORE_NAME, new List<string> { "widget_1", "widget_2" }, parallelism: 1);
record Widget(string Size, string Color);
``` ```
{{% /codetab %}} {{% /codetab %}}
@ -791,44 +767,36 @@ Below are code examples that leverage Dapr SDKs for performing state transaction
{{% codetab %}} {{% codetab %}}
```csharp ```csharp
//dependencies
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Dapr.Client; using Dapr.Client;
using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks;
using System.Threading;
using System.Text.Json;
//code const string DAPR_STORE_NAME = "statestore";
namespace EventService
var builder = WebApplication.CreateBuilder(args);
builder.Serivces.AddDaprClient();
var app = builder.Build();
//Resolve the DaprClient from the dependency injection registration
using var client = app.Services.GetRequiredService<DaprClient>();
var random = new Random();
while (true)
{ {
class Program await Task.Delay(TimeSpan.FromSeconds(5));
{ var orderId = random.Next(1, 1000);
static async Task Main(string[] args) var requests = new List<StateTransactionRequest>
{
string DAPR_STORE_NAME = "statestore";
while(true) {
System.Threading.Thread.Sleep(5000);
Random random = new Random();
int orderId = random.Next(1,1000);
using var client = new DaprClientBuilder().Build();
var requests = new List<StateTransactionRequest>()
{ {
new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert), new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert),
new StateTransactionRequest("order_2", null, StateOperationType.Delete) new StateTransactionRequest("order_2", null, StateOperationType.Delete)
}; };
CancellationTokenSource source = new CancellationTokenSource(); var cancellationTokenSource = new CancellationTokenSource();
CancellationToken cancellationToken = source.Token; var cancellationToken = cancellationTokenSource.Token;
//Using Dapr SDK to perform the state transactions
//Use the DaprClient to perform the state transactions
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken); await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken);
Console.WriteLine("Order requested: " + orderId); Console.WriteLine($"Order requested: {orderId}");
Console.WriteLine("Result: " + result); Console.WriteLine($"Result: {result}");
}
}
}
} }
``` ```