3
3
using System ;
4
4
using System . Net ;
5
5
using System . Text ;
6
+ using System . Threading ;
6
7
using System . Threading . Tasks ;
7
8
8
9
namespace IpcServiceSample . ConsoleClient
@@ -18,58 +19,75 @@ private static async Task MainAsync(string[] args)
18
19
{
19
20
try
20
21
{
21
- IpcServiceClient < IComputingService > computingClient = new IpcServiceClientBuilder < IComputingService > ( )
22
- . UseNamedPipe ( "pipeName" )
23
- . Build ( ) ;
22
+ Console . WriteLine ( "Press any key to stop." ) ;
23
+ var source = new CancellationTokenSource ( ) ;
24
24
25
- IpcServiceClient < ISystemService > systemClient = new IpcServiceClientBuilder < ISystemService > ( )
26
- . UseTcp ( IPAddress . Loopback , 45684 )
27
- . Build ( ) ;
28
-
29
- // test 1: call IPC service method with primitive types
30
- float result1 = await computingClient . InvokeAsync ( x => x . AddFloat ( 1.23f , 4.56f ) ) ;
31
- Console . WriteLine ( $ "[TEST 1] sum of 2 floating number is: { result1 } ") ;
32
-
33
- // test 2: call IPC service method with complex types
34
- ComplexNumber result2 = await computingClient . InvokeAsync ( x => x . AddComplexNumber (
35
- new ComplexNumber ( 0.1f , 0.3f ) ,
36
- new ComplexNumber ( 0.2f , 0.6f ) ) ) ;
37
- Console . WriteLine ( $ "[TEST 2] sum of 2 complexe number is: { result2 . A } +{ result2 . B } i") ;
38
-
39
- // test 3: call IPC service method with an array of complex types
40
- ComplexNumber result3 = await computingClient . InvokeAsync ( x => x . AddComplexNumbers ( new [ ]
25
+ await Task . WhenAll ( RunTestsAsync ( source . Token ) , Task . Run ( ( ) =>
41
26
{
42
- new ComplexNumber ( 0.5f , 0.4f ) ,
43
- new ComplexNumber ( 0.2f , 0.1f ) ,
44
- new ComplexNumber ( 0.3f , 0.5f ) ,
27
+ Console . ReadKey ( ) ;
28
+ Console . WriteLine ( "Cancelling..." ) ;
29
+ source . Cancel ( ) ;
45
30
} ) ) ;
46
- Console . WriteLine ( $ "[TEST 3] sum of 3 complexe number is: { result3 . A } +{ result3 . B } i") ;
31
+ }
32
+ catch ( Exception ex )
33
+ {
34
+ Console . WriteLine ( ex ) ;
35
+ }
36
+ }
47
37
48
- // test 4: call IPC service method without parameter or return
49
- await systemClient . InvokeAsync ( x => x . DoNothing ( ) ) ;
50
- Console . WriteLine ( $ "[TEST 4] invoked DoNothing()") ;
38
+ private static async Task RunTestsAsync ( CancellationToken cancellationToken )
39
+ {
40
+ IpcServiceClient < IComputingService > computingClient = new IpcServiceClientBuilder < IComputingService > ( )
41
+ . UseNamedPipe ( "pipeName" )
42
+ . Build ( ) ;
51
43
52
- // test 5: call IPC service method with enum parameter
53
- string text = await systemClient . InvokeAsync ( x => x . ConvertText ( "hEllO woRd!" , TextStyle . Upper ) ) ;
54
- Console . WriteLine ( $ "[TEST 5] { text } " ) ;
44
+ IpcServiceClient < ISystemService > systemClient = new IpcServiceClientBuilder < ISystemService > ( )
45
+ . UseTcp ( IPAddress . Loopback , 45684 )
46
+ . Build ( ) ;
55
47
56
- // test 6 : call IPC service method returning GUID
57
- Guid generatedId = await systemClient . InvokeAsync ( x => x . GenerateId ( ) ) ;
58
- Console . WriteLine ( $ "[TEST 6] generated ID is: { generatedId } ") ;
48
+ // test 1 : call IPC service method with primitive types
49
+ float result1 = await computingClient . InvokeAsync ( x => x . AddFloat ( 1.23f , 4.56f ) , cancellationToken ) ;
50
+ Console . WriteLine ( $ "[TEST 1] sum of 2 floating number is: { result1 } ") ;
59
51
60
- // test 7: call IPC service method with byte array
61
- byte [ ] input = Encoding . UTF8 . GetBytes ( "Test" ) ;
62
- byte [ ] reversed = await systemClient . InvokeAsync ( x => x . ReverseBytes ( input ) ) ;
63
- Console . WriteLine ( $ "[TEST 7] reversed bytes are: { Convert . ToBase64String ( reversed ) } ") ;
52
+ // test 2: call IPC service method with complex types
53
+ ComplexNumber result2 = await computingClient . InvokeAsync ( x => x . AddComplexNumber (
54
+ new ComplexNumber ( 0.1f , 0.3f ) ,
55
+ new ComplexNumber ( 0.2f , 0.6f ) ) , cancellationToken ) ;
56
+ Console . WriteLine ( $ "[TEST 2] sum of 2 complexe number is: { result2 . A } +{ result2 . B } i") ;
64
57
65
- // test 8: call IPC service method with generic parameter
66
- string print = await systemClient . InvokeAsync ( x => x . Printout ( DateTime . UtcNow ) ) ;
67
- Console . WriteLine ( $ "[TEST 8] print out value: { print } ") ;
68
- }
69
- catch ( Exception ex )
58
+ // test 3: call IPC service method with an array of complex types
59
+ ComplexNumber result3 = await computingClient . InvokeAsync ( x => x . AddComplexNumbers ( new [ ]
70
60
{
71
- Console . WriteLine ( ex ) ;
72
- }
61
+ new ComplexNumber ( 0.5f , 0.4f ) ,
62
+ new ComplexNumber ( 0.2f , 0.1f ) ,
63
+ new ComplexNumber ( 0.3f , 0.5f ) ,
64
+ } ) , cancellationToken ) ;
65
+ Console . WriteLine ( $ "[TEST 3] sum of 3 complexe number is: { result3 . A } +{ result3 . B } i", cancellationToken ) ;
66
+
67
+ // test 4: call IPC service method without parameter or return
68
+ await systemClient . InvokeAsync ( x => x . DoNothing ( ) , cancellationToken ) ;
69
+ Console . WriteLine ( $ "[TEST 4] invoked DoNothing()") ;
70
+
71
+ // test 5: call IPC service method with enum parameter
72
+ string text = await systemClient . InvokeAsync ( x => x . ConvertText ( "hEllO woRd!" , TextStyle . Upper ) , cancellationToken ) ;
73
+ Console . WriteLine ( $ "[TEST 5] { text } ") ;
74
+
75
+ // test 6: call IPC service method returning GUID
76
+ Guid generatedId = await systemClient . InvokeAsync ( x => x . GenerateId ( ) , cancellationToken ) ;
77
+ Console . WriteLine ( $ "[TEST 6] generated ID is: { generatedId } ") ;
78
+
79
+ // test 7: call IPC service method with byte array
80
+ byte [ ] input = Encoding . UTF8 . GetBytes ( "Test" ) ;
81
+ byte [ ] reversed = await systemClient . InvokeAsync ( x => x . ReverseBytes ( input ) , cancellationToken ) ;
82
+ Console . WriteLine ( $ "[TEST 7] reversed bytes are: { Convert . ToBase64String ( reversed ) } ") ;
83
+
84
+ // test 8: call IPC service method with generic parameter
85
+ string print = await systemClient . InvokeAsync ( x => x . Printout ( DateTime . UtcNow ) , cancellationToken ) ;
86
+ Console . WriteLine ( $ "[TEST 8] print out value: { print } ") ;
87
+
88
+ // test 9: call slow IPC service method
89
+ await systemClient . InvokeAsync ( x => x . SlowOperation ( ) , cancellationToken ) ;
90
+ Console . WriteLine ( $ "[TEST 9] Called slow operation") ;
73
91
}
74
92
}
75
93
}
0 commit comments