44using System . Linq ;
55using System . Threading . Tasks ;
66using IdGen ;
7+ using Microsoft . Extensions . DependencyInjection ;
8+ using Microsoft . Extensions . Hosting ;
79using Microsoft . Extensions . Logging ;
810using Orleans ;
11+ using Orleans . Configuration ;
12+ using Orleans . Hosting ;
913using Transfer . IGrains . Common ;
1014using Transfer . IGrains . DTx ;
1115
@@ -14,10 +18,11 @@ namespace Transfer.Client
1418 internal class Program
1519 {
1620 private static readonly IdGenerator IdGen = new IdGenerator ( 0 ) ;
21+ private static IHost host ;
1722
1823 private static async Task Main ( string [ ] args )
1924 {
20- using var client = await StartClientWithRetries ( ) ;
25+ var client = await StartClientWithRetries ( ) ;
2126
2227 while ( true )
2328 {
@@ -31,7 +36,13 @@ private static async Task Main(string[] args)
3136 {
3237 await DTx ( client ) ;
3338 }
39+ else
40+ {
41+ break ;
42+ }
3443 }
44+
45+ host . Dispose ( ) ;
3546 }
3647
3748 private static async Task Normal ( IClusterClient client )
@@ -46,23 +57,28 @@ private static async Task Normal(IClusterClient client)
4657 var topupTaskList = new List < Task > ( ) ;
4758 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
4859 {
49- topupTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x => client . GetGrain < IAccount > ( account ) . TopUp ( 100 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
60+ topupTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x =>
61+ client . GetGrain < IAccount > ( account ) . TopUp ( 100 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
5062 }
5163
5264 topupWatch . Start ( ) ;
5365 await Task . WhenAll ( topupTaskList ) ;
5466 topupWatch . Stop ( ) ;
55- Console . WriteLine ( $ "{ times * accountCount } Recharge completed, taking: { topupWatch . ElapsedMilliseconds } ms") ;
67+ Console . WriteLine (
68+ $ "{ times * accountCount } Recharge completed, taking: { topupWatch . ElapsedMilliseconds } ms") ;
5669 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
5770 {
58- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account ) . GetBalance ( ) } ") ;
71+ Console . WriteLine (
72+ $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account ) . GetBalance ( ) } ") ;
5973 }
6074
6175 var transferWatch = new Stopwatch ( ) ;
6276 var transferTaskList = new List < Task > ( ) ;
6377 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
6478 {
65- transferTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x => client . GetGrain < IAccount > ( account ) . Transfer ( account + accountCount , 50 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
79+ transferTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x =>
80+ client . GetGrain < IAccount > ( account )
81+ . Transfer ( account + accountCount , 50 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
6682 }
6783
6884 transferWatch . Start ( ) ;
@@ -72,12 +88,14 @@ private static async Task Normal(IClusterClient client)
7288 $ "{ times * accountCount } The transfer is completed, taking: { transferWatch . ElapsedMilliseconds } ms") ;
7389 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
7490 {
75- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account ) . GetBalance ( ) } ") ;
91+ Console . WriteLine (
92+ $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account ) . GetBalance ( ) } ") ;
7693 }
7794
7895 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
7996 {
80- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account + accountCount ) . GetBalance ( ) } ") ;
97+ Console . WriteLine (
98+ $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account + accountCount ) . GetBalance ( ) } ") ;
8199 }
82100 }
83101 catch ( Exception e )
@@ -98,16 +116,19 @@ private static async Task DTx(IClusterClient client)
98116 var topupTaskList = new List < Task > ( ) ;
99117 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
100118 {
101- topupTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x => client . GetGrain < IDTxAccount > ( account ) . TopUp ( 100 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
119+ topupTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x =>
120+ client . GetGrain < IDTxAccount > ( account ) . TopUp ( 100 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
102121 }
103122
104123 topupWatch . Start ( ) ;
105124 await Task . WhenAll ( topupTaskList ) ;
106125 topupWatch . Stop ( ) ;
107- Console . WriteLine ( $ "{ times * accountCount } Recharge completed, taking: { topupWatch . ElapsedMilliseconds } ms") ;
126+ Console . WriteLine (
127+ $ "{ times * accountCount } Recharge completed, taking: { topupWatch . ElapsedMilliseconds } ms") ;
108128 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
109129 {
110- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account ) . GetBalance ( ) } ") ;
130+ Console . WriteLine (
131+ $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account ) . GetBalance ( ) } ") ;
111132 }
112133
113134 var transferWatch = new Stopwatch ( ) ;
@@ -132,12 +153,14 @@ private static async Task DTx(IClusterClient client)
132153 $ "{ times * accountCount } The transfer is completed, taking: { transferWatch . ElapsedMilliseconds } ms") ;
133154 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
134155 {
135- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account ) . GetBalance ( ) } ") ;
156+ Console . WriteLine (
157+ $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account ) . GetBalance ( ) } ") ;
136158 }
137159
138160 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
139161 {
140- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account + accountCount ) . GetBalance ( ) } ") ;
162+ Console . WriteLine (
163+ $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account + accountCount ) . GetBalance ( ) } ") ;
141164 }
142165 }
143166 catch ( Exception e )
@@ -154,13 +177,20 @@ private static async Task<IClusterClient> StartClientWithRetries(int initializeA
154177 {
155178 try
156179 {
157- var builder = new ClientBuilder ( )
158- . UseLocalhostClustering ( )
159- . ConfigureApplicationParts ( parts =>
160- parts . AddApplicationPart ( typeof ( IAccount ) . Assembly ) . WithReferences ( ) )
180+ var builder = new HostBuilder ( )
181+ . UseOrleansClient ( clientBuilder =>
182+ {
183+ clientBuilder . UseLocalhostClustering ( ) ;
184+ clientBuilder . Configure < ClusterOptions > ( options =>
185+ {
186+ options . ClusterId = "dev" ;
187+ options . ServiceId = "Transfer" ;
188+ } ) ;
189+ } )
161190 . ConfigureLogging ( logging => logging . AddConsole ( ) ) ;
162- client = builder . Build ( ) ;
163- await client . Connect ( ) ;
191+ host = builder . Build ( ) ;
192+ await host . StartAsync ( ) ;
193+ client = host . Services . GetService < IClusterClient > ( ) ;
164194 Console . WriteLine ( "Client successfully connect to silo host" ) ;
165195 break ;
166196 }
@@ -181,4 +211,4 @@ private static async Task<IClusterClient> StartClientWithRetries(int initializeA
181211 return client ;
182212 }
183213 }
184- }
214+ }
0 commit comments