diff --git a/daprdocs/content/en/developing-applications/building-blocks/configuration/howto-manage-configuration.md b/daprdocs/content/en/developing-applications/building-blocks/configuration/howto-manage-configuration.md index 4a7e2e224..5b663c3ea 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/configuration/howto-manage-configuration.md +++ b/daprdocs/content/en/developing-applications/building-blocks/configuration/howto-manage-configuration.md @@ -76,27 +76,21 @@ The following example shows how to get a saved configuration item using the Dapr {{% codetab %}} ```csharp -//dependencies using System; using System.Collections.Generic; using System.Threading.Tasks; using Dapr.Client; -//code -namespace ConfigurationApi -{ - public class Program - { - private static readonly string CONFIG_STORE_NAME = "configstore"; +const string CONFIG_STORE_NAME = "configstore"; - public static async Task Main(string[] args) - { - using var client = new DaprClientBuilder().Build(); - var configuration = await client.GetConfiguration(CONFIG_STORE_NAME, new List() { "orderId1", "orderId2" }); - Console.WriteLine($"Got key=\n{configuration[0].Key} -> {configuration[0].Value}\n{configuration[1].Key} -> {configuration[1].Value}"); - } - } -} +var builder = WebApplication.CreateBuilder(args); +builder.Services.AddDaprClient(); +var app = builder.Build(); + +using var client = app.Services.GetRequiredServices(); + +var configuration = await client.GetConfiguration(CONFIG_STORE_NAME, [ "orderId1", "orderId2" ]); +Console.WriteLine($"Got key=\n{configuration[0].Key} -> {configuration[0].Value}\n{configuration[1].Key} -> {configuration[1].Value}"); ``` {{% /codetab %}} @@ -261,13 +255,19 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Dapr.Client; +using System.Text.Json; const string DAPR_CONFIGURATION_STORE = "configstore"; var CONFIGURATION_ITEMS = new List { "orderId1", "orderId2" }; -var client = new DaprClientBuilder().Build(); + +var builder = WebApplication.CreateBuilder(args); +builder.Services.AddDaprClient(); +var app = builder.Build(); + +var client = app.Services.GetRequiredService(); // Subscribe for configuration changes -SubscribeConfigurationResponse subscribe = await client.SubscribeConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS); +var subscribe = await client.SubscribeConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS); // Print configuration changes await foreach (var items in subscribe.Source) @@ -279,7 +279,7 @@ await foreach (var items in subscribe.Source) subscriptionId = subscribe.Id; continue; } - var cfg = System.Text.Json.JsonSerializer.Serialize(items); + var cfg = JsonSerializer.Serialize(items); Console.WriteLine("Configuration update " + cfg); } ``` @@ -303,40 +303,23 @@ using Dapr.Extensions.Configuration; using System.Collections.Generic; using System.Threading; -namespace ConfigurationApi -{ - public class Program - { - public static void Main(string[] args) - { - Console.WriteLine("Starting application."); - CreateHostBuilder(args).Build().Run(); - Console.WriteLine("Closing application."); - } +Console.WriteLine("Starting application."); +var builder = WebApplication.CreateBuilder(args); - /// - /// Creates WebHost Builder. - /// - /// Arguments. - /// Returns IHostbuilder. - public static IHostBuilder CreateHostBuilder(string[] args) - { - var client = new DaprClientBuilder().Build(); - return Host.CreateDefaultBuilder(args) - .ConfigureAppConfiguration(config => - { - // Get the initial value and continue to watch it for changes. - config.AddDaprConfigurationStore("configstore", new List() { "orderId1","orderId2" }, client, TimeSpan.FromSeconds(20)); - config.AddStreamingDaprConfigurationStore("configstore", new List() { "orderId1","orderId2" }, client, TimeSpan.FromSeconds(20)); +// Unlike most other situations, we build a `DaprClient` here using its factory because we cannot rely on `IConfiguration` +// or other injected services to configure it because we haven't yet built the DI container. +var client = new DaprClientBuilder().Build(); - }) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } - } -} +// In a real-world application, you'd also add the following line to register the `DaprClient` with the DI container so +// it can be injected into other services. In this demonstration, it's not necessary as we're not injecting it anywhere. +// builder.Services.AddDaprClient(); + +// Get the initial value and continue to watch it for changes +builder.Configuration.AddDaprConfigurationStore("configstore", new List() { "orderId1","orderId2" }, client, TimeSpan.FromSeconds(20)); +builder.Configuration.AddStreamingDaprConfigurationStore("configstore", new List() { "orderId1","orderId2" }, client, TimeSpan.FromSeconds(20)); + +await builder.Build().RunAsync(); +Console.WriteLine("Closing application."); ``` Navigate to the directory containing the above code, then run the following command to launch both a Dapr sidecar and the subscriber application: @@ -524,29 +507,23 @@ Following are the code examples showing how you can unsubscribe to configuration {{< tabs ".NET" Java Python Go JavaScript "HTTP API (BASH)" "HTTP API (Powershell)">}} {{% codetab %}} + ```csharp using System; using System.Collections.Generic; using System.Threading.Tasks; using Dapr.Client; -const string DAPR_CONFIGURATION_STORE = "configstore"; -var client = new DaprClientBuilder().Build(); +var builder = WebApplication.CreateBuilder(); +builder.Services.AddDaprClient(); +var app = builder.Build(); -// Unsubscribe to config updates and exit the app -async Task unsubscribe(string subscriptionId) -{ - try - { - await client.UnsubscribeConfiguration(DAPR_CONFIGURATION_STORE, subscriptionId); - Console.WriteLine("App unsubscribed from config changes"); - Environment.Exit(0); - } - catch (Exception ex) - { - Console.WriteLine("Error unsubscribing from config updates: " + ex.Message); - } -} +const string DAPR_CONFIGURATION_STORE = "configstore"; +const string SubscriptionId = "abc123"; //Replace with the subscription identifier to unsubscribe from +var client = app.Services.GetRequiredService(); + +await client.UnsubscribeConfiguration(DAPR_CONFIGURATION_STORE, SubscriptionId); +Console.WriteLine("App unsubscribed from config changes"); ``` {{% /codetab %}}