|
11 | 11 | // limitations under the License. |
12 | 12 | // ------------------------------------------------------------------------ |
13 | 13 |
|
14 | | -using Dapr.Actors.Communication; |
| 14 | +using System; |
| 15 | +using System.Threading; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using Dapr.Actors; |
| 18 | +using Dapr.Actors.Client; |
15 | 19 | using IDemoActor; |
16 | 20 |
|
17 | | -namespace ActorClient |
| 21 | +var data = new MyData("ValueA", "ValueB"); |
| 22 | + |
| 23 | +// Create an actor Id. |
| 24 | +var actorId = new ActorId("abc"); |
| 25 | + |
| 26 | +// Make strongly typed Actor calls with Remoting. |
| 27 | +// DemoActor is the type registered with Dapr runtime in the service. |
| 28 | +var proxy = ActorProxy.Create<IDemoActor.IDemoActor>(actorId, "DemoActor"); |
| 29 | + |
| 30 | +Console.WriteLine("Making call using actor proxy to save data."); |
| 31 | +await proxy.SaveData(data, TimeSpan.FromMinutes(10)); |
| 32 | +Console.WriteLine("Making call using actor proxy to get data."); |
| 33 | +var receivedData = await proxy.GetData(); |
| 34 | +Console.WriteLine($"Received data is {receivedData}."); |
| 35 | + |
| 36 | +// Making some more calls to test methods. |
| 37 | +try |
| 38 | +{ |
| 39 | + Console.WriteLine("Making calls to an actor method which has no argument and no return type."); |
| 40 | + await proxy.TestNoArgumentNoReturnType(); |
| 41 | +} |
| 42 | +catch (Exception ex) |
| 43 | +{ |
| 44 | + Console.WriteLine($"ERROR: Got exception while making call to method with No Argument & No Return Type. Exception: {ex}"); |
| 45 | +} |
| 46 | + |
| 47 | +try |
| 48 | +{ |
| 49 | + await proxy.TestThrowException(); |
| 50 | +} |
| 51 | +catch (ActorMethodInvocationException ex) |
18 | 52 | { |
19 | | - using System; |
20 | | - using System.Threading; |
21 | | - using System.Threading.Tasks; |
22 | | - using Dapr.Actors; |
23 | | - using Dapr.Actors.Client; |
24 | | - |
25 | | - /// <summary> |
26 | | - /// Actor Client class. |
27 | | - /// </summary> |
28 | | - public class Program |
| 53 | + if (ex.InnerException is ActorInvokeException invokeEx && invokeEx.ActualExceptionType is "System.NotImplementedException") |
29 | 54 | { |
30 | | - /// <summary> |
31 | | - /// Entry point. |
32 | | - /// </summary> |
33 | | - /// <param name="args">Arguments.</param> |
34 | | - /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> |
35 | | - public static async Task Main(string[] args) |
36 | | - { |
37 | | - var data = new MyData() |
38 | | - { |
39 | | - PropertyA = "ValueA", |
40 | | - PropertyB = "ValueB", |
41 | | - }; |
42 | | - |
43 | | - // Create an actor Id. |
44 | | - var actorId = new ActorId("abc"); |
45 | | - |
46 | | - // Make strongly typed Actor calls with Remoting. |
47 | | - // DemoActor is the type registered with Dapr runtime in the service. |
48 | | - var proxy = ActorProxy.Create<IDemoActor.IDemoActor>(actorId, "DemoActor"); |
49 | | - |
50 | | - Console.WriteLine("Making call using actor proxy to save data."); |
51 | | - await proxy.SaveData(data, TimeSpan.FromMinutes(10)); |
52 | | - Console.WriteLine("Making call using actor proxy to get data."); |
53 | | - var receivedData = await proxy.GetData(); |
54 | | - Console.WriteLine($"Received data is {receivedData}."); |
55 | | - |
56 | | - // Making some more calls to test methods. |
57 | | - try |
58 | | - { |
59 | | - Console.WriteLine("Making calls to an actor method which has no argument and no return type."); |
60 | | - await proxy.TestNoArgumentNoReturnType(); |
61 | | - } |
62 | | - catch (Exception ex) |
63 | | - { |
64 | | - Console.WriteLine($"ERROR: Got exception while making call to method with No Argument & No Return Type. Exception: {ex}"); |
65 | | - } |
66 | | - |
67 | | - try |
68 | | - { |
69 | | - await proxy.TestThrowException(); |
70 | | - } |
71 | | - catch (ActorMethodInvocationException ex) |
72 | | - { |
73 | | - if (ex.InnerException is ActorInvokeException invokeEx && invokeEx.ActualExceptionType is "System.NotImplementedException") |
74 | | - { |
75 | | - Console.WriteLine($"Got Correct Exception from actor method invocation."); |
76 | | - } |
77 | | - else |
78 | | - { |
79 | | - Console.WriteLine($"Got Incorrect Exception from actor method invocation. Exception {ex.InnerException}"); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - // Making calls without Remoting, this shows method invocation using InvokeMethodAsync methods, the method name and its payload is provided as arguments to InvokeMethodAsync methods. |
84 | | - Console.WriteLine("Making calls without Remoting."); |
85 | | - var nonRemotingProxy = ActorProxy.Create(actorId, "DemoActor"); |
86 | | - await nonRemotingProxy.InvokeMethodAsync("TestNoArgumentNoReturnType"); |
87 | | - await nonRemotingProxy.InvokeMethodAsync("SaveData", data); |
88 | | - await nonRemotingProxy.InvokeMethodAsync<MyData>("GetData"); |
89 | | - |
90 | | - Console.WriteLine("Registering the timer and reminder"); |
91 | | - await proxy.RegisterTimer(); |
92 | | - await proxy.RegisterReminder(); |
93 | | - Console.WriteLine("Waiting so the timer and reminder can be triggered"); |
94 | | - await Task.Delay(6000); |
95 | | - |
96 | | - Console.WriteLine("Making call using actor proxy to get data after timer and reminder triggered"); |
97 | | - receivedData = await proxy.GetData(); |
98 | | - Console.WriteLine($"Received data is {receivedData}."); |
99 | | - |
100 | | - Console.WriteLine("Getting details of the registered reminder"); |
101 | | - var reminder = await proxy.GetReminder(); |
102 | | - Console.WriteLine($"Received reminder is {reminder}."); |
103 | | - |
104 | | - Console.WriteLine("Deregistering timer. Timers would any way stop if the actor is deactivated as part of Dapr garbage collection."); |
105 | | - await proxy.UnregisterTimer(); |
106 | | - Console.WriteLine("Deregistering reminder. Reminders are durable and would not stop until an explicit deregistration or the actor is deleted."); |
107 | | - await proxy.UnregisterReminder(); |
| 55 | + Console.WriteLine($"Got Correct Exception from actor method invocation."); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + Console.WriteLine($"Got Incorrect Exception from actor method invocation. Exception {ex.InnerException}"); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Making calls without Remoting, this shows method invocation using InvokeMethodAsync methods, the method name and its payload is provided as arguments to InvokeMethodAsync methods. |
| 64 | +Console.WriteLine("Making calls without Remoting."); |
| 65 | +var nonRemotingProxy = ActorProxy.Create(actorId, "DemoActor"); |
| 66 | +await nonRemotingProxy.InvokeMethodAsync("TestNoArgumentNoReturnType"); |
| 67 | +await nonRemotingProxy.InvokeMethodAsync("SaveData", data); |
| 68 | +await nonRemotingProxy.InvokeMethodAsync<MyData>("GetData"); |
| 69 | + |
| 70 | +Console.WriteLine("Registering the timer and reminder"); |
| 71 | +await proxy.RegisterTimer(); |
| 72 | +await proxy.RegisterReminder(); |
| 73 | +Console.WriteLine("Waiting so the timer and reminder can be triggered"); |
| 74 | +await Task.Delay(6000); |
| 75 | + |
| 76 | +Console.WriteLine("Making call using actor proxy to get data after timer and reminder triggered"); |
| 77 | +receivedData = await proxy.GetData(); |
| 78 | +Console.WriteLine($"Received data is {receivedData}."); |
| 79 | + |
| 80 | +Console.WriteLine("Getting details of the registered reminder"); |
| 81 | +var reminder = await proxy.GetReminder(); |
| 82 | +Console.WriteLine($"Received reminder is {reminder}."); |
| 83 | + |
| 84 | +Console.WriteLine("Deregistering timer. Timers would any way stop if the actor is deactivated as part of Dapr garbage collection."); |
| 85 | +await proxy.UnregisterTimer(); |
| 86 | +Console.WriteLine("Deregistering reminder. Reminders are durable and would not stop until an explicit deregistration or the actor is deleted."); |
| 87 | +await proxy.UnregisterReminder(); |
108 | 88 |
|
109 | | - Console.WriteLine("Registering reminder with repetitions - The reminder will repeat 3 times."); |
110 | | - await proxy.RegisterReminderWithRepetitions(3); |
111 | | - Console.WriteLine("Waiting so the reminder can be triggered"); |
112 | | - await Task.Delay(5000); |
113 | | - Console.WriteLine("Getting details of the registered reminder"); |
114 | | - reminder = await proxy.GetReminder(); |
115 | | - Console.WriteLine($"Received reminder is {reminder?.ToString() ?? "None"} (expecting None)."); |
116 | | - Console.WriteLine("Registering reminder with ttl and repetitions, i.e. reminder stops when either condition is met - The reminder will repeat 2 times."); |
117 | | - await proxy.RegisterReminderWithTtlAndRepetitions(TimeSpan.FromSeconds(5), 2); |
118 | | - Console.WriteLine("Getting details of the registered reminder"); |
119 | | - reminder = await proxy.GetReminder(); |
120 | | - Console.WriteLine($"Received reminder is {reminder}."); |
121 | | - Console.WriteLine("Deregistering reminder. Reminders are durable and would not stop until an explicit deregistration or the actor is deleted."); |
122 | | - await proxy.UnregisterReminder(); |
123 | | - |
124 | | - Console.WriteLine("Registering reminder and Timer with TTL - The reminder will self delete after 10 seconds."); |
125 | | - await proxy.RegisterReminderWithTtl(TimeSpan.FromSeconds(10)); |
126 | | - await proxy.RegisterTimerWithTtl(TimeSpan.FromSeconds(10)); |
127 | | - Console.WriteLine("Getting details of the registered reminder"); |
128 | | - reminder = await proxy.GetReminder(); |
129 | | - Console.WriteLine($"Received reminder is {reminder}."); |
130 | | - |
131 | | - // Track the reminder. |
132 | | - var timer = new Timer(async state => Console.WriteLine($"Received data: {await proxy.GetData()}"), null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5)); |
133 | | - await Task.Delay(TimeSpan.FromSeconds(21)); |
134 | | - await timer.DisposeAsync(); |
135 | | - |
136 | | - Console.WriteLine("Creating a Bank Actor"); |
137 | | - var bank = ActorProxy.Create<IBankActor>(ActorId.CreateRandom(), "DemoActor"); |
138 | | - while (true) |
139 | | - { |
140 | | - var balance = await bank.GetAccountBalance(); |
141 | | - Console.WriteLine($"Balance for account '{balance.AccountId}' is '{balance.Balance:c}'."); |
142 | | - |
143 | | - Console.WriteLine($"Withdrawing '{10m:c}'..."); |
144 | | - try |
145 | | - { |
146 | | - await bank.Withdraw(new WithdrawRequest() { Amount = 10m, }); |
147 | | - } |
148 | | - catch (ActorMethodInvocationException ex) |
149 | | - { |
150 | | - Console.WriteLine("Overdraft: " + ex.Message); |
151 | | - break; |
152 | | - } |
153 | | - } |
154 | | - } |
| 89 | +Console.WriteLine("Registering reminder with repetitions - The reminder will repeat 3 times."); |
| 90 | +await proxy.RegisterReminderWithRepetitions(3); |
| 91 | +Console.WriteLine("Waiting so the reminder can be triggered"); |
| 92 | +await Task.Delay(5000); |
| 93 | +Console.WriteLine("Getting details of the registered reminder"); |
| 94 | +reminder = await proxy.GetReminder(); |
| 95 | +Console.WriteLine($"Received reminder is {reminder?.ToString() ?? "None"} (expecting None)."); |
| 96 | +Console.WriteLine("Registering reminder with ttl and repetitions, i.e. reminder stops when either condition is met - The reminder will repeat 2 times."); |
| 97 | +await proxy.RegisterReminderWithTtlAndRepetitions(TimeSpan.FromSeconds(5), 2); |
| 98 | +Console.WriteLine("Getting details of the registered reminder"); |
| 99 | +reminder = await proxy.GetReminder(); |
| 100 | +Console.WriteLine($"Received reminder is {reminder}."); |
| 101 | +Console.WriteLine("Deregistering reminder. Reminders are durable and would not stop until an explicit deregistration or the actor is deleted."); |
| 102 | +await proxy.UnregisterReminder(); |
| 103 | + |
| 104 | +Console.WriteLine("Registering reminder and Timer with TTL - The reminder will self delete after 10 seconds."); |
| 105 | +await proxy.RegisterReminderWithTtl(TimeSpan.FromSeconds(10)); |
| 106 | +await proxy.RegisterTimerWithTtl(TimeSpan.FromSeconds(10)); |
| 107 | +Console.WriteLine("Getting details of the registered reminder"); |
| 108 | +reminder = await proxy.GetReminder(); |
| 109 | +Console.WriteLine($"Received reminder is {reminder}."); |
| 110 | + |
| 111 | +// Track the reminder. |
| 112 | +var timer = new Timer(async state => Console.WriteLine($"Received data: {await proxy.GetData()}"), null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5)); |
| 113 | +await Task.Delay(TimeSpan.FromSeconds(21)); |
| 114 | +await timer.DisposeAsync(); |
| 115 | + |
| 116 | +Console.WriteLine("Creating a Bank Actor"); |
| 117 | +var bank = ActorProxy.Create<IBankActor>(ActorId.CreateRandom(), "DemoActor"); |
| 118 | +while (true) |
| 119 | +{ |
| 120 | + var balance = await bank.GetAccountBalance(); |
| 121 | + Console.WriteLine($"Balance for account '{balance.AccountId}' is '{balance.Balance:c}'."); |
| 122 | + |
| 123 | + Console.WriteLine($"Withdrawing '{10m:c}'..."); |
| 124 | + try |
| 125 | + { |
| 126 | + await bank.Withdraw(new WithdrawRequest(10m)); |
| 127 | + } |
| 128 | + catch (ActorMethodInvocationException ex) |
| 129 | + { |
| 130 | + Console.WriteLine($"Overdraft: {ex.Message}"); |
| 131 | + break; |
155 | 132 | } |
156 | 133 | } |
0 commit comments