Getting started with the Dapr client .NET SDK

How to get up and running with the Dapr .NET SDK

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

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

Related links

Last modified January 1, 0001