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 . Hosting ;
912using Transfer . IGrains . Common ;
1013using Transfer . IGrains . DTx ;
1114
@@ -14,10 +17,11 @@ namespace Transfer.Client
1417 internal class Program
1518 {
1619 private static readonly IdGenerator IdGen = new IdGenerator ( 0 ) ;
20+ private static IHost _host ;
1721
1822 private static async Task Main ( string [ ] args )
1923 {
20- using var client = await StartClientWithRetries ( ) ;
24+ var client = await StartClientWithRetries ( ) ;
2125
2226 while ( true )
2327 {
@@ -31,7 +35,12 @@ private static async Task Main(string[] args)
3135 {
3236 await DTx ( client ) ;
3337 }
38+ else
39+ {
40+ break ;
41+ }
3442 }
43+ _host . Dispose ( ) ;
3544 }
3645
3746 private static async Task Normal ( IClusterClient client )
@@ -46,23 +55,28 @@ private static async Task Normal(IClusterClient client)
4655 var topupTaskList = new List < Task > ( ) ;
4756 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
4857 {
49- topupTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x => client . GetGrain < IAccount > ( account ) . TopUp ( 100 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
58+ topupTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x =>
59+ client . GetGrain < IAccount > ( account ) . TopUp ( 100 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
5060 }
5161
5262 topupWatch . Start ( ) ;
5363 await Task . WhenAll ( topupTaskList ) ;
5464 topupWatch . Stop ( ) ;
55- Console . WriteLine ( $ "{ times * accountCount } Recharge completed, taking: { topupWatch . ElapsedMilliseconds } ms") ;
65+ Console . WriteLine (
66+ $ "{ times * accountCount } Recharge completed, taking: { topupWatch . ElapsedMilliseconds } ms") ;
5667 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
5768 {
58- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account ) . GetBalance ( ) } ") ;
69+ Console . WriteLine (
70+ $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account ) . GetBalance ( ) } ") ;
5971 }
6072
6173 var transferWatch = new Stopwatch ( ) ;
6274 var transferTaskList = new List < Task > ( ) ;
6375 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
6476 {
65- transferTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x => client . GetGrain < IAccount > ( account ) . Transfer ( account + accountCount , 50 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
77+ transferTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x =>
78+ client . GetGrain < IAccount > ( account )
79+ . Transfer ( account + accountCount , 50 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
6680 }
6781
6882 transferWatch . Start ( ) ;
@@ -72,12 +86,14 @@ private static async Task Normal(IClusterClient client)
7286 $ "{ times * accountCount } The transfer is completed, taking: { transferWatch . ElapsedMilliseconds } ms") ;
7387 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
7488 {
75- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account ) . GetBalance ( ) } ") ;
89+ Console . WriteLine (
90+ $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account ) . GetBalance ( ) } ") ;
7691 }
7792
7893 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
7994 {
80- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account + accountCount ) . GetBalance ( ) } ") ;
95+ Console . WriteLine (
96+ $ "The balance of account { account } is{ await client . GetGrain < IAccount > ( account + accountCount ) . GetBalance ( ) } ") ;
8197 }
8298 }
8399 catch ( Exception e )
@@ -98,16 +114,19 @@ private static async Task DTx(IClusterClient client)
98114 var topupTaskList = new List < Task > ( ) ;
99115 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
100116 {
101- topupTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x => client . GetGrain < IDTxAccount > ( account ) . TopUp ( 100 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
117+ topupTaskList . AddRange ( Enumerable . Range ( 0 , times ) . Select ( x =>
118+ client . GetGrain < IDTxAccount > ( account ) . TopUp ( 100 , IdGen . CreateId ( ) . ToString ( ) ) ) ) ;
102119 }
103120
104121 topupWatch . Start ( ) ;
105122 await Task . WhenAll ( topupTaskList ) ;
106123 topupWatch . Stop ( ) ;
107- Console . WriteLine ( $ "{ times * accountCount } Recharge completed, taking: { topupWatch . ElapsedMilliseconds } ms") ;
124+ Console . WriteLine (
125+ $ "{ times * accountCount } Recharge completed, taking: { topupWatch . ElapsedMilliseconds } ms") ;
108126 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
109127 {
110- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account ) . GetBalance ( ) } ") ;
128+ Console . WriteLine (
129+ $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account ) . GetBalance ( ) } ") ;
111130 }
112131
113132 var transferWatch = new Stopwatch ( ) ;
@@ -132,12 +151,14 @@ private static async Task DTx(IClusterClient client)
132151 $ "{ times * accountCount } The transfer is completed, taking: { transferWatch . ElapsedMilliseconds } ms") ;
133152 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
134153 {
135- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account ) . GetBalance ( ) } ") ;
154+ Console . WriteLine (
155+ $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account ) . GetBalance ( ) } ") ;
136156 }
137157
138158 foreach ( var account in Enumerable . Range ( 0 , accountCount ) )
139159 {
140- Console . WriteLine ( $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account + accountCount ) . GetBalance ( ) } ") ;
160+ Console . WriteLine (
161+ $ "The balance of account { account } is{ await client . GetGrain < IDTxAccount > ( account + accountCount ) . GetBalance ( ) } ") ;
141162 }
142163 }
143164 catch ( Exception e )
@@ -154,13 +175,13 @@ private static async Task<IClusterClient> StartClientWithRetries(int initializeA
154175 {
155176 try
156177 {
157- var builder = new ClientBuilder ( )
158- . UseLocalhostClustering ( )
159- . ConfigureApplicationParts ( parts =>
160- parts . AddApplicationPart ( typeof ( IAccount ) . Assembly ) . WithReferences ( ) )
178+ var builder = new HostBuilder ( )
179+ . UseOrleansClient ( clientBuilder => clientBuilder . UseLocalhostClustering ( ) )
180+ // .ConfigureApplicationParts(parts =>
181+ // parts.AddApplicationPart(typeof(IAccount).Assembly).WithReferences())
161182 . ConfigureLogging ( logging => logging . AddConsole ( ) ) ;
162- client = builder . Build ( ) ;
163- await client . Connect ( ) ;
183+ _host = builder . Build ( ) ;
184+ client = _host . Services . GetService < IClusterClient > ( ) ;
164185 Console . WriteLine ( "Client successfully connect to silo host" ) ;
165186 break ;
166187 }
@@ -181,4 +202,4 @@ private static async Task<IClusterClient> StartClientWithRetries(int initializeA
181202 return client ;
182203 }
183204 }
184- }
205+ }
0 commit comments