Client

The Dapr client package allows you to interact with other Dapr applications from a .NET application.

Prerequisites

Building blocks

The .NET SDK allows you to interface with all of the Dapr building blocks.

Invoke a service

You can either use the DaprClient or System.Net.Http.HttpClient to invoke your services.

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

Save & get application state

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

Query 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. }

Publish messages

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

Interact with output bindings

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

Retrieve secrets

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

Get Configuration Keys (Alpha)

  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. }