// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
namespace DaprClient
{
using System;
using System.Text.Json;
using System.Threading.Tasks;
using Dapr.Client;
using Dapr.Client.Http;
///
/// Shows Dapr client calls.
///
public class Program
{
private static readonly string stateKeyName = "mykey";
private static readonly string storeName = "statestore";
private static readonly string pubsubName = "pubsub";
///
/// Main entry point.
///
/// Arguments.
/// A representing the asynchronous operation.
public static async Task Main(string[] args)
{
var jsonOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
};
var client = new DaprClientBuilder()
.UseJsonSerializationOptions(jsonOptions)
.Build();
await PublishEventAsync(client);
// Save State
await SaveStateAsync(client);
// Read State
await GetStateAsync(client);
// Delete State
await DeleteStateAsync(client);
#region Service Invoke - Required RoutingService
//// This provides an example of how to invoke a method on another REST service that is listening on http.
//// To use it run RoutingService in this solution.
//// Invoke deposit operation on RoutingSample service by publishing event.
//await PublishDepositeEventToRoutingSampleAsync(client);
//await Task.Delay(TimeSpan.FromSeconds(1));
//await DepositUsingServiceInvocation(client);
////Invoke deposit operation on RoutingSample service by POST.
//await InvokeWithdrawServiceOperationAsync(client);
////Invoke deposit operation on RoutingSample service by GET.
//await InvokeBalanceServiceOperationAsync(client);
#endregion
Console.WriteLine("Done");
}
internal static async Task PublishDepositeEventToRoutingSampleAsync(DaprClient client)
{
var eventData = new { Id = "17", Amount = (decimal)10, };
await client.PublishEventAsync(pubsubName, "deposit", eventData);
Console.WriteLine("Published deposit event!");
}
internal static async Task PublishEventAsync(DaprClient client)
{
var eventData = new Widget() { Size = "small", Color = "yellow", };
await client.PublishEventAsync(pubsubName, "TopicA", eventData);
Console.WriteLine("Published Event!");
}
internal static async Task SaveStateAsync(DaprClient client)
{
var state = new Widget() { Size = "small", Color = "yellow", };
await client.SaveStateAsync(storeName, stateKeyName, state);
Console.WriteLine("Saved State!");
}
internal static async Task GetStateAsync(DaprClient client)
{
var state = await client.GetStateAsync(storeName, stateKeyName);
if (state == null)
{
Console.WriteLine("State not found in store");
}
else
{
Console.WriteLine($"Got State: {state.Size} {state.Color}");
}
}
internal static async Task DeleteStateAsync(DaprClient client)
{
await client.DeleteStateAsync(storeName, stateKeyName);
Console.WriteLine("Deleted State!");
}
internal static async Task DepositUsingServiceInvocation(DaprClient client)
{
Console.WriteLine("DepositUsingServiceInvocation");
var data = new { id = "17", amount = (decimal)99 };
HTTPExtension httpExtension = new HTTPExtension()
{
Verb = HTTPVerb.Post
};
// Invokes a POST method named "depoit" that takes input of type "Transaction" as define in the RoutingSample.
Console.WriteLine("invoking");
var a = await client.InvokeMethodAsync