From 756e72b52ebb0b350149ba2cebb8bab57b41809e Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Fri, 18 Apr 2025 18:56:36 -0500 Subject: [PATCH 1/3] Modernized the .NET examples for interacting with state in Dapr Signed-off-by: Whit Waldo --- .../state-management/howto-get-save-state.md | 194 ++++++++---------- 1 file changed, 81 insertions(+), 113 deletions(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md index e630365db3f..bef395089bb 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md +++ b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md @@ -72,38 +72,27 @@ The following example shows how to save and retrieve a single key/value pair usi ```csharp -//dependencies -using System; -using System.Collections.Generic; -using System.Net.Http; -using System.Net.Http.Headers; +using System.Text; using System.Threading.Tasks; using Dapr.Client; -using Microsoft.AspNetCore.Mvc; -using System.Threading; -using System.Text.Json; -//code -namespace EventService +var builder = WebApplication.CreateBuilder(args); +builder.Serivces.AddDaprClient(); +var app = builder.Build(); + +var random = new Random(); +//Resolve the DaprClient from its dependency injection registration +using var client = app.Services.GetRequiredService(); + +while(true) { - class Program - { - 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 - await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString()); - await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString()); - var result = await client.GetStateAsync(DAPR_STORE_NAME, "order_1"); - Console.WriteLine("Result after get: " + result); - } - } - } + await Task.Delay(TimeSpan.FromSeconds(5)); + var orderId = random.Next(1,1000); + //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_2", orderId.ToString()); + var result = await client.GetStateAsync(DAPR_STORE_NAME, "order_1"); + Console.WriteLine($"Result after get: {result}"); } ``` @@ -359,23 +348,20 @@ Below are code examples that leverage Dapr SDKs for deleting the state. {{% codetab %}} ```csharp -//dependencies using Dapr.Client; +using System.Threading.Tasks; -//code -namespace EventService -{ - class Program - { - static async Task Main(string[] args) - { - string DAPR_STORE_NAME = "statestore"; - //Using Dapr SDK to delete the state - using var client = new DaprClientBuilder().Build(); - await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken); - } - } -} +const string DAPR_STORE_NAME = "statestore"; + +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(); + +//Use the DaprClient to delete the state +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: @@ -540,22 +526,19 @@ Below are code examples that leverage Dapr SDKs for saving and retrieving multip {{% codetab %}} ```csharp -//dependencies using Dapr.Client; -//code -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 multipleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List { "order_1", "order_2" }, parallelism: 1); - } - } -} +using System.Threading.Tasks; + +const string DAPR_STORE_NAME = "statestore"; + +var builder = WebApplication.CreateBuilder(args); +builder.Services.AddDaprClient(); +var app = builder.Build(); + +//Resolve the DaprClient from the dependency injection registration +using var client = app.Services.GetRequiredService(); + +IReadOnlyList multipleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List { "order_1", "order_2" }, parallelism: 1); ``` 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: ```csharp -//dependencies using Dapr.Client; -//code -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> mulitpleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List { "widget_1", "widget_2" }, parallelism: 1); - } - } +using System.Threading.Tasks; - class Widget - { - string Size { get; set; } - string Color { get; set; } - } -} +const string DAPR_STORE_NAME = "statestore"; + +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(); + +IReadOnlyList> mulitpleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List { "widget_1", "widget_2" }, parallelism: 1); + +record Widget(string Size, string Color); ``` {{% /codetab %}} @@ -791,44 +767,36 @@ Below are code examples that leverage Dapr SDKs for performing state transaction {{% codetab %}} ```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 Microsoft.AspNetCore.Mvc; -using System.Threading; -using System.Text.Json; +using System.Threading.Tasks; -//code -namespace EventService +const string DAPR_STORE_NAME = "statestore"; + +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(); + +var random = new Random(); + +while (true) { - class Program - { - static async Task Main(string[] args) - { - 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() - { - new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert), - new StateTransactionRequest("order_2", null, StateOperationType.Delete) - }; - CancellationTokenSource source = new CancellationTokenSource(); - CancellationToken cancellationToken = source.Token; - //Using Dapr SDK to perform the state transactions - await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken); - Console.WriteLine("Order requested: " + orderId); - Console.WriteLine("Result: " + result); - } - } - } + await Task.Delay(TimeSpan.FromSeconds(5)); + var orderId = random.Next(1, 1000); + var requests = new List + { + new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert), + new StateTransactionRequest("order_2", null, StateOperationType.Delete) + }; + var cancellationTokenSource = new CancellationTokenSource(); + var cancellationToken = cancellationTokenSource.Token; + + //Use the DaprClient to perform the state transactions + await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken); + Console.WriteLine($"Order requested: {orderId}"); + Console.WriteLine($"Result: {result}"); } ``` From 67afbc4631eb0b3356b813710b7a25f25cc400ea Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Mon, 21 Apr 2025 13:37:43 -0500 Subject: [PATCH 2/3] Update daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md Co-authored-by: Mark Fussell Signed-off-by: Whit Waldo --- .../building-blocks/state-management/howto-get-save-state.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md index bef395089bb..67884eeb0d3 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md +++ b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md @@ -77,7 +77,7 @@ using System.Threading.Tasks; using Dapr.Client; var builder = WebApplication.CreateBuilder(args); -builder.Serivces.AddDaprClient(); +builder.Services.AddDaprClient(); var app = builder.Build(); var random = new Random(); From 01608eb94d81b70b892dbbebb3334cc73e828fb3 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Mon, 21 Apr 2025 13:38:20 -0500 Subject: [PATCH 3/3] Update daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md Co-authored-by: Mark Fussell Signed-off-by: Whit Waldo --- .../building-blocks/state-management/howto-get-save-state.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md index 67884eeb0d3..727ea207c4f 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md +++ b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md @@ -354,7 +354,7 @@ using System.Threading.Tasks; const string DAPR_STORE_NAME = "statestore"; var builder = WebApplication.CreateBuilder(args); -builder.Serivces.AddDaprClient(); +builder.Services.AddDaprClient(); var app = builder.Build(); //Resolve the DaprClient from the dependency injection registration