Client

Dapr 客户端包允许您从 .NET 应用程序中与其他 Dapr 应用程序进行交互。

注意

如果你还没有,请尝试使用一个快速入门快速了解如何使用 Dapr .NET SDK 与一个 API 构建块。

构建块

.NET SDK 允许您与所有的Dapr构建块进行接口交互。

调用服务

您可以使用 DaprClientSystem.Net.Http.HttpClient 来调用您的服务。

  1. using var client = new DaprClientBuilder().Build();
  2. // Invokes a POST method named "deposit" that takes input of type "Transaction"
  3. var data = new { id = "17", amount = 99m };
  4. var account = await client.InvokeMethodAsync<object, Account>("routing", "deposit", data, cancellationToken);
  5. Console.WriteLine("Returned: id:{0} | Balance:{1}", account.Id, account.Balance);
  1. var client = DaprClient.CreateInvokeHttpClient(appId: "routing");
  2. var deposit = new Transaction { Id = "17", Amount = 99m };
  3. var response = await client.PostAsJsonAsync("/deposit", deposit, cancellationToken);
  4. var account = await response.Content.ReadFromJsonAsync<Account>(cancellationToken: cancellationToken);
  5. Console.WriteLine("Returned: id:{0} | Balance:{1}", account.Id, account.Balance);

保存和获取应用程序状态

  1. var client = new DaprClientBuilder().Build();
  2. var state = new Widget() { Size = "small", Color = "yellow", };
  3. await client.SaveStateAsync(storeName, stateKeyName, state, cancellationToken: cancellationToken);
  4. Console.WriteLine("Saved State!");
  5. state = await client.GetStateAsync<Widget>(storeName, stateKeyName, cancellationToken: cancellationToken);
  6. Console.WriteLine($"Got State: {state.Size} {state.Color}");
  7. await client.DeleteStateAsync(storeName, stateKeyName, cancellationToken: cancellationToken);
  8. Console.WriteLine("Deleted State!");

查询状态(Alpha)

  1. var query = "{" +
  2. "\"filter\": {" +
  3. "\"EQ\": { \"value.Id\": \"1\" }" +
  4. "}," +
  5. "\"sort\": [" +
  6. "{" +
  7. "\"key\": \"value.Balance\"," +
  8. "\"order\": \"DESC\"" +
  9. "}" +
  10. "]" +
  11. "}";
  12. var client = new DaprClientBuilder().Build();
  13. var queryResponse = await client.QueryStateAsync<Account>("querystore", query, cancellationToken: cancellationToken);
  14. Console.WriteLine($"Got {queryResponse.Results.Count}");
  15. foreach (var account in queryResponse.Results)
  16. {
  17. Console.WriteLine($"Account: {account.Data.Id} has {account.Data.Balance}");
  18. }

发布消息

  1. var client = new DaprClientBuilder().Build();
  2. var eventData = new { Id = "17", Amount = 10m, };
  3. await client.PublishEventAsync(pubsubName, "deposit", eventData, cancellationToken);
  4. Console.WriteLine("Published deposit event!");

与输出绑定交互

  1. using var client = new DaprClientBuilder().Build();
  2. // Example payload for the Twilio SendGrid binding
  3. var email = new
  4. {
  5. metadata = new
  6. {
  7. emailTo = "customer@example.com",
  8. subject = "An email from Dapr SendGrid binding",
  9. },
  10. data = "<h1>Testing Dapr Bindings</h1>This is a test.<br>Bye!",
  11. };
  12. await client.InvokeBindingAsync("send-email", "create", email);

检索密钥

  1. var client = new DaprClientBuilder().Build();
  2. // Retrieve a key-value-pair-based secret - returns a Dictionary<string, string>
  3. var secrets = await client.GetSecretAsync("mysecretstore", "key-value-pair-secret");
  4. Console.WriteLine($"Got secret keys: {string.Join(", ", secrets.Keys)}");
  1. var client = new DaprClientBuilder().Build();
  2. // Retrieve a key-value-pair-based secret - returns a Dictionary<string, string>
  3. var secrets = await client.GetSecretAsync("mysecretstore", "key-value-pair-secret");
  4. Console.WriteLine($"Got secret keys: {string.Join(", ", secrets.Keys)}");
  5. // Retrieve a single-valued secret - returns a Dictionary<string, string>
  6. // containing a single value with the secret name as the key
  7. var data = await client.GetSecretAsync("mysecretstore", "single-value-secret");
  8. var value = data["single-value-secret"]
  9. Console.WriteLine("Got a secret value, I'm not going to be print it, it's a secret!");

获取配置键

  1. var client = new DaprClientBuilder().Build();
  2. // Retrieve a specific set of keys.
  3. var specificItems = await client.GetConfiguration("configstore", new List<string>() { "key1", "key2" });
  4. Console.WriteLine($"Here are my values:\n{specificItems[0].Key} -> {specificItems[0].Value}\n{specificItems[1].Key} -> {specificItems[1].Value}");
  5. // Retrieve all configuration items by providing an empty list.
  6. var specificItems = await client.GetConfiguration("configstore", new List<string>());
  7. Console.WriteLine($"I got {configItems.Count} entires!");
  8. foreach (var item in configItems)
  9. {
  10. Console.WriteLine($"{item.Key} -> {item.Value}")
  11. }

订阅配置键

  1. var client = new DaprClientBuilder().Build();
  2. // The Subscribe Configuration API returns a wrapper around an IAsyncEnumerable<IEnumerable<ConfigurationItem>>.
  3. // Iterate through it by accessing its Source in a foreach loop. The loop will end when the stream is severed
  4. // or if the cancellation token is cancelled.
  5. var subscribeConfigurationResponse = await daprClient.SubscribeConfiguration(store, keys, metadata, cts.Token);
  6. await foreach (var items in subscribeConfigurationResponse.Source.WithCancellation(cts.Token))
  7. {
  8. foreach (var item in items)
  9. {
  10. Console.WriteLine($"{item.Key} -> {item.Value}")
  11. }
  12. }

分布式锁(Alpha)

获取锁

  1. using System;
  2. using Dapr.Client;
  3. namespace LockService
  4. {
  5. class Program
  6. {
  7. [Obsolete("Distributed Lock API is in Alpha, this can be removed once it is stable.")]
  8. static async Task Main(string[] args)
  9. {
  10. var daprLockName = "lockstore";
  11. var fileName = "my_file_name";
  12. var client = new DaprClientBuilder().Build();
  13. // Locking with this approach will also unlock it automatically, as this is a disposable object
  14. await using (var fileLock = await client.Lock(DAPR_LOCK_NAME, fileName, "random_id_abc123", 60))
  15. {
  16. if (fileLock.Success)
  17. {
  18. Console.WriteLine("Success");
  19. }
  20. else
  21. {
  22. Console.WriteLine($"Failed to lock {fileName}.");
  23. }
  24. }
  25. }
  26. }
  27. }

释放现有锁

  1. using System;
  2. using Dapr.Client;
  3. namespace LockService
  4. {
  5. class Program
  6. {
  7. static async Task Main(string[] args)
  8. {
  9. var daprLockName = "lockstore";
  10. var client = new DaprClientBuilder().Build();
  11. var response = await client.Unlock(DAPR_LOCK_NAME, "my_file_name", "random_id_abc123"));
  12. Console.WriteLine(response.status);
  13. }
  14. }
  15. }

管理工作流实例(Alpha)

  1. var daprClient = new DaprClientBuilder().Build();
  2. string instanceId = "MyWorkflowInstance1";
  3. string workflowComponentName = "dapr"; // alternatively, this could be the name of a workflow component defined in yaml
  4. string workflowName = "MyWorkflowDefinition";
  5. var input = new { name = "Billy", age = 30 }; // Any JSON-serializable value is OK
  6. // Start workflow
  7. var startResponse = await daprClient.StartWorkflowAsync(instanceId, workflowComponentName, workflowName, input);
  8. // Terminate workflow
  9. await daprClient.TerminateWorkflowAsync(instanceId, workflowComponentName);
  10. // Get workflow metadata
  11. var getResponse = await daprClient.GetWorkflowAsync(instanceId, workflowComponentName, workflowName);

Sidecar APIs

Sidecar 健康

.NET SDK 提供了一种方式来轮询 sidecar 的健康状态,以及一个方便的方法来等待 sidecar 就绪。

健康轮询

当旁车和您的应用程序都处于运行状态(完全初始化)时,此健康端点返回true。

  1. var client = new DaprClientBuilder().Build();
  2. var isDaprReady = await client.CheckHealthAsync();
  3. if (isDaprReady)
  4. {
  5. // Execute Dapr dependent code.
  6. }

健康轮询 (出站)

当 Dapr 初始化所有组件时,此健康端点返回 true,但可能尚未完成与您的应用程序建立通信渠道的设置。

当您希望在启动路径中利用Dapr组件时,这是最佳选择,例如从secretstore加载密钥。

  1. var client = new DaprClientBuilder().Build();
  2. var isDaprComponentsReady = await client.CheckOutboundHealthAsync();
  3. if (isDaprComponentsReady)
  4. {
  5. // Execute Dapr component dependent code.
  6. }

等待 sidecar

DaprClient还提供了一个辅助方法,用于等待sidecar变为健康状态(仅适用于组件)。 在使用该方法时,建议包括一个CancellationToken以便请求超时。 下面是一个示例,展示了如何在DaprSecretStoreConfigurationProvider中使用它。

  1. // Wait for the Dapr sidecar to report healthy before attempting use Dapr components.
  2. using (var tokenSource = new CancellationTokenSource(sidecarWaitTimeout))
  3. {
  4. await client.WaitForSidecarAsync(tokenSource.Token);
  5. }
  6. // Perform Dapr component operations here i.e. fetching secrets.

关闭 sidecar

  1. var client = new DaprClientBuilder().Build();
  2. await client.ShutdownSidecarAsync();

相关链接