1 #region Copyright notice and license
2 // Copyright 2015-2016 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 #endregion
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
20 using Grpc.Core;
21 using Grpc.Core.Utils;
22 
23 namespace Math
24 {
25     public static class MathExamples
26     {
DivExample(Math.MathClient client)27         public static void DivExample(Math.MathClient client)
28         {
29             DivReply result = client.Div(new DivArgs { Dividend = 10, Divisor = 3 });
30             Console.WriteLine("Div Result: " + result);
31         }
32 
DivAsyncExample(Math.MathClient client)33         public static async Task DivAsyncExample(Math.MathClient client)
34         {
35             DivReply result = await client.DivAsync(new DivArgs { Dividend = 4, Divisor = 5 });
36             Console.WriteLine("DivAsync Result: " + result);
37         }
38 
FibExample(Math.MathClient client)39         public static async Task FibExample(Math.MathClient client)
40         {
41             using (var call = client.Fib(new FibArgs { Limit = 5 }))
42             {
43                 List<Num> result = await call.ResponseStream.ToListAsync();
44                 Console.WriteLine("Fib Result: " + string.Join("|", result));
45             }
46         }
47 
SumExample(Math.MathClient client)48         public static async Task SumExample(Math.MathClient client)
49         {
50             var numbers = new List<Num>
51             {
52                 new Num { Num_ = 1 },
53                 new Num { Num_ = 2 },
54                 new Num { Num_ = 3 }
55             };
56 
57             using (var call = client.Sum())
58             {
59                 await call.RequestStream.WriteAllAsync(numbers);
60                 Console.WriteLine("Sum Result: " + await call.ResponseAsync);
61             }
62         }
63 
DivManyExample(Math.MathClient client)64         public static async Task DivManyExample(Math.MathClient client)
65         {
66             var divArgsList = new List<DivArgs>
67             {
68                 new DivArgs { Dividend = 10, Divisor = 3 },
69                 new DivArgs { Dividend = 100, Divisor = 21 },
70                 new DivArgs { Dividend = 7, Divisor = 2 }
71             };
72 
73             using (var call = client.DivMany())
74             {
75                 await call.RequestStream.WriteAllAsync(divArgsList);
76                 Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToListAsync()));
77             }
78         }
79 
DependentRequestsExample(Math.MathClient client)80         public static async Task DependentRequestsExample(Math.MathClient client)
81         {
82             var numbers = new List<Num>
83             {
84                 new Num { Num_ = 1 },
85                 new Num { Num_ = 2 },
86                 new Num { Num_ = 3 }
87             };
88 
89             Num sum;
90             using (var sumCall = client.Sum())
91             {
92                 await sumCall.RequestStream.WriteAllAsync(numbers);
93                 sum = await sumCall.ResponseAsync;
94             }
95 
96             DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count });
97             Console.WriteLine("Avg Result: " + result);
98         }
99 
100         /// <summary>
101         /// Shows how to handle a call ending with non-OK status.
102         /// </summary>
HandleErrorExample(Math.MathClient client)103         public static async Task HandleErrorExample(Math.MathClient client)
104         {
105             try
106             {
107                  DivReply result = await client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 });
108             }
109             catch (RpcException ex)
110             {
111                 Console.WriteLine(string.Format("RPC ended with status {0}", ex.Status));
112             }
113         }
114 
115         /// <summary>
116         /// Shows how to send request headers and how to access response headers
117         /// and response trailers.
118         /// </summary>
MetadataExample(Math.MathClient client)119         public static async Task MetadataExample(Math.MathClient client)
120         {
121             var requestHeaders = new Metadata
122             {
123                 { "custom-header", "custom-value" }
124             };
125 
126             var call = client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }, requestHeaders);
127 
128             // Get response headers
129             Metadata responseHeaders = await call.ResponseHeadersAsync;
130 
131             var result = await call;
132 
133             // Get response trailers after the call has finished.
134             Metadata responseTrailers = call.GetTrailers();
135         }
136     }
137 }
138