Dapr 客户端 .NET SDK入门

如何启动和运行Dapr .NET SDK

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

先决条件

构建块

.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!");

发布消息

  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!");

相关链接