mirror of https://github.com/dapr/docs.git
Merge branch 'v1.15' into patch-1
This commit is contained in:
commit
e31ecda757
|
|
@ -76,27 +76,21 @@ The following example shows how to get a saved configuration item using the Dapr
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//dependencies
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
|
||||||
//code
|
const string CONFIG_STORE_NAME = "configstore";
|
||||||
namespace ConfigurationApi
|
|
||||||
{
|
|
||||||
public class Program
|
|
||||||
{
|
|
||||||
private static readonly string CONFIG_STORE_NAME = "configstore";
|
|
||||||
|
|
||||||
public static async Task Main(string[] args)
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
{
|
builder.Services.AddDaprClient();
|
||||||
using var client = new DaprClientBuilder().Build();
|
var app = builder.Build();
|
||||||
var configuration = await client.GetConfiguration(CONFIG_STORE_NAME, new List<string>() { "orderId1", "orderId2" });
|
|
||||||
Console.WriteLine($"Got key=\n{configuration[0].Key} -> {configuration[0].Value}\n{configuration[1].Key} -> {configuration[1].Value}");
|
using var client = app.Services.GetRequiredServices<DaprClient>();
|
||||||
}
|
|
||||||
}
|
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 %}}
|
{{% /codetab %}}
|
||||||
|
|
@ -261,13 +255,19 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
const string DAPR_CONFIGURATION_STORE = "configstore";
|
const string DAPR_CONFIGURATION_STORE = "configstore";
|
||||||
var CONFIGURATION_ITEMS = new List<string> { "orderId1", "orderId2" };
|
var CONFIGURATION_ITEMS = new List<string> { "orderId1", "orderId2" };
|
||||||
var client = new DaprClientBuilder().Build();
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
builder.Services.AddDaprClient();
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
var client = app.Services.GetRequiredService<DaprClient>();
|
||||||
|
|
||||||
// Subscribe for configuration changes
|
// 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
|
// Print configuration changes
|
||||||
await foreach (var items in subscribe.Source)
|
await foreach (var items in subscribe.Source)
|
||||||
|
|
@ -279,7 +279,7 @@ await foreach (var items in subscribe.Source)
|
||||||
subscriptionId = subscribe.Id;
|
subscriptionId = subscribe.Id;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
var cfg = System.Text.Json.JsonSerializer.Serialize(items);
|
var cfg = JsonSerializer.Serialize(items);
|
||||||
Console.WriteLine("Configuration update " + cfg);
|
Console.WriteLine("Configuration update " + cfg);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
@ -303,40 +303,23 @@ using Dapr.Extensions.Configuration;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace ConfigurationApi
|
Console.WriteLine("Starting application.");
|
||||||
{
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
public class Program
|
|
||||||
{
|
|
||||||
public static void Main(string[] args)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Starting application.");
|
|
||||||
CreateHostBuilder(args).Build().Run();
|
|
||||||
Console.WriteLine("Closing application.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
// Unlike most other situations, we build a `DaprClient` here using its factory because we cannot rely on `IConfiguration`
|
||||||
/// Creates WebHost Builder.
|
// or other injected services to configure it because we haven't yet built the DI container.
|
||||||
/// </summary>
|
var client = new DaprClientBuilder().Build();
|
||||||
/// <param name="args">Arguments.</param>
|
|
||||||
/// <returns>Returns IHostbuilder.</returns>
|
|
||||||
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<string>() { "orderId1","orderId2" }, client, TimeSpan.FromSeconds(20));
|
|
||||||
config.AddStreamingDaprConfigurationStore("configstore", new List<string>() { "orderId1","orderId2" }, client, TimeSpan.FromSeconds(20));
|
|
||||||
|
|
||||||
})
|
// In a real-world application, you'd also add the following line to register the `DaprClient` with the DI container so
|
||||||
.ConfigureWebHostDefaults(webBuilder =>
|
// it can be injected into other services. In this demonstration, it's not necessary as we're not injecting it anywhere.
|
||||||
{
|
// builder.Services.AddDaprClient();
|
||||||
webBuilder.UseStartup<Startup>();
|
|
||||||
});
|
// Get the initial value and continue to watch it for changes
|
||||||
}
|
builder.Configuration.AddDaprConfigurationStore("configstore", new List<string>() { "orderId1","orderId2" }, client, TimeSpan.FromSeconds(20));
|
||||||
}
|
builder.Configuration.AddStreamingDaprConfigurationStore("configstore", new List<string>() { "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:
|
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)">}}
|
{{< tabs ".NET" Java Python Go JavaScript "HTTP API (BASH)" "HTTP API (Powershell)">}}
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
|
||||||
const string DAPR_CONFIGURATION_STORE = "configstore";
|
var builder = WebApplication.CreateBuilder();
|
||||||
var client = new DaprClientBuilder().Build();
|
builder.Services.AddDaprClient();
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
// Unsubscribe to config updates and exit the app
|
const string DAPR_CONFIGURATION_STORE = "configstore";
|
||||||
async Task unsubscribe(string subscriptionId)
|
const string SubscriptionId = "abc123"; //Replace with the subscription identifier to unsubscribe from
|
||||||
{
|
var client = app.Services.GetRequiredService<DaprClient>();
|
||||||
try
|
|
||||||
{
|
await client.UnsubscribeConfiguration(DAPR_CONFIGURATION_STORE, SubscriptionId);
|
||||||
await client.UnsubscribeConfiguration(DAPR_CONFIGURATION_STORE, subscriptionId);
|
Console.WriteLine("App unsubscribed from config changes");
|
||||||
Console.WriteLine("App unsubscribed from config changes");
|
|
||||||
Environment.Exit(0);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Error unsubscribing from config updates: " + ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue