6
6
namespace DaprClient
7
7
{
8
8
using System ;
9
- using System . Collections . Generic ;
10
- using System . Threading ;
9
+ using System . Text . Json ;
11
10
using System . Threading . Tasks ;
12
11
using Dapr . Client ;
12
+ using Dapr . Client . Http ;
13
13
14
14
/// <summary>
15
15
/// Shows Dapr client calls.
@@ -26,7 +26,15 @@ public class Program
26
26
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
27
27
public static async Task Main ( string [ ] args )
28
28
{
29
- var client = new DaprClientBuilder ( ) . Build ( ) ;
29
+ var jsonOptions = new JsonSerializerOptions ( )
30
+ {
31
+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
32
+ PropertyNameCaseInsensitive = true ,
33
+ } ;
34
+
35
+ var client = new DaprClientBuilder ( )
36
+ . UseJsonSerializationOptions ( jsonOptions )
37
+ . Build ( ) ;
30
38
31
39
await PublishEventAsync ( client ) ;
32
40
@@ -42,26 +50,31 @@ public static async Task Main(string[] args)
42
50
#region Service Invoke - Required RoutingService
43
51
// This provides an example of how to invoke a method on another REST service that is listening on http.
44
52
// To use it run RoutingService in this solution.
45
- // Invoke deposit operation on RoutingSample service by publishing event.
53
+ // Invoke deposit operation on RoutingSample service by publishing event.
54
+
46
55
//await PublishDepositeEventToRoutingSampleAsync(client);
56
+
57
+ //await Task.Delay(TimeSpan.FromSeconds(1));
47
58
48
- // Invoke deposit operation on RoutingSample service by POST.
59
+ //await DepositUsingServiceInvocation(client);
60
+
61
+ //Invoke deposit operation on RoutingSample service by POST.
49
62
//await InvokeWithdrawServiceOperationAsync(client);
50
63
51
- // Invoke deposit operation on RoutingSample service by GET.
64
+ //Invoke deposit operation on RoutingSample service by GET.
52
65
//await InvokeBalanceServiceOperationAsync(client);
53
66
#endregion
54
67
68
+ Console . WriteLine ( "Done" ) ;
55
69
}
56
70
57
71
internal static async Task PublishDepositeEventToRoutingSampleAsync ( DaprClient client )
58
72
{
59
- var eventData = new { id = "17" , amount = ( decimal ) 10 , } ;
73
+ var eventData = new { Id = "17" , Amount = ( decimal ) 10 , } ;
60
74
await client . PublishEventAsync ( "deposit" , eventData ) ;
61
75
Console . WriteLine ( "Published deposit event!" ) ;
62
76
}
63
77
64
-
65
78
internal static async Task PublishEventAsync ( DaprClient client )
66
79
{
67
80
var eventData = new Widget ( ) { Size = "small" , Color = "yellow" , } ;
@@ -95,6 +108,24 @@ internal static async Task DeleteStateAsync(DaprClient client)
95
108
Console . WriteLine ( "Deleted State!" ) ;
96
109
}
97
110
111
+ internal static async Task DepositUsingServiceInvocation ( DaprClient client )
112
+ {
113
+ Console . WriteLine ( "DepositUsingServiceInvocation" ) ;
114
+ var data = new { id = "17" , amount = ( decimal ) 99 } ;
115
+
116
+ HTTPExtension httpExtension = new HTTPExtension ( )
117
+ {
118
+ Verb = HTTPVerb . Post
119
+ } ;
120
+
121
+ // Invokes a POST method named "depoit" that takes input of type "Transaction" as define in the RoutingSample.
122
+ Console . WriteLine ( "invoking" ) ;
123
+
124
+ var a = await client . InvokeMethodAsync < object , Account > ( "routing" , "deposit" , data , httpExtension ) ;
125
+ Console . WriteLine ( "Returned: id:{0} | Balance:{1}" , a . Id , a . Balance ) ;
126
+
127
+ Console . WriteLine ( "Completed" ) ;
128
+ }
98
129
99
130
/// <summary>
100
131
/// This example shows how to invoke a POST method on a service listening on http.
@@ -107,14 +138,16 @@ internal static async Task DeleteStateAsync(DaprClient client)
107
138
/// <returns></returns>
108
139
internal static async Task InvokeWithdrawServiceOperationAsync ( DaprClient client )
109
140
{
141
+ Console . WriteLine ( "Invoking withdraw" ) ;
110
142
var data = new { id = "17" , amount = ( decimal ) 10 , } ;
111
143
112
- // Add the verb to metadata if the method is other than a POST
113
- var metaData = new Dictionary < string , string > ( ) ;
114
- metaData . Add ( "http.verb" , "POST" ) ;
144
+ HTTPExtension httpExtension = new HTTPExtension ( )
145
+ {
146
+ Verb = HTTPVerb . Post
147
+ } ;
115
148
116
- // Invokes a POST method named "Withdraw" that takes input of type "Transaction" as define in the RoutingSample.
117
- await client . InvokeMethodAsync < object > ( "routing" , "Withdraw" , data , metaData ) ;
149
+ // Invokes a POST method named "Withdraw" that takes input of type "Transaction" as define in the RoutingSample.
150
+ await client . InvokeMethodAsync < object > ( "routing" , "Withdraw" , data , httpExtension ) ;
118
151
119
152
Console . WriteLine ( "Completed" ) ;
120
153
}
@@ -130,28 +163,16 @@ internal static async Task InvokeWithdrawServiceOperationAsync(DaprClient client
130
163
/// <returns></returns>
131
164
internal static async Task InvokeBalanceServiceOperationAsync ( DaprClient client )
132
165
{
133
- // Add the verb to metadata if the method is other than a POST
134
- var metaData = new Dictionary < string , string > ( ) ;
135
- metaData . Add ( "http.verb" , "GET" ) ;
166
+ Console . WriteLine ( "Invoking balance" ) ;
136
167
137
168
// Invokes a GET method named "hello" that takes input of type "MyData" and returns a string.
138
- var res = await client . InvokeMethodAsync < object > ( "routing" , "17" , metaData ) ;
139
-
140
- Console . WriteLine ( $ "Received balance { res } ") ;
141
- }
142
-
143
- /// <summary>
144
- /// This example shows how to invoke a method on a service listening on gRPC.
145
- /// </summary>
146
- /// <param name="client"></param>
147
- /// <returns></returns>
148
- internal static async Task InvokeMethodOnGrpcServiceAsync ( DaprClient client )
149
- {
150
- MyData data = new MyData ( ) { Message = "mydata" } ;
169
+ HTTPExtension httpExtension = new HTTPExtension ( )
170
+ {
171
+ Verb = HTTPVerb . Get
172
+ } ;
173
+ var res = await client . InvokeMethodAsync < Account > ( "routing" , "17" , httpExtension ) ;
151
174
152
- // invokes a method named "hello" that takes input of type "MyData" and returns a string.
153
- string s = await client . InvokeMethodAsync < MyData , string > ( "nodeapp" , "hello" , data ) ;
154
- Console . WriteLine ( "received {0}" , s ) ;
175
+ Console . WriteLine ( $ "Received balance { res . Balance } ") ;
155
176
}
156
177
157
178
private class Widget
@@ -167,5 +188,14 @@ public MyData()
167
188
168
189
public String Message { get ; set ; }
169
190
}
191
+
192
+
193
+ internal class Account
194
+ {
195
+ public string Id { get ; set ; }
196
+
197
+ public decimal Balance { get ; set ; }
198
+ }
170
199
}
171
200
}
201
+
0 commit comments