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 using (var call = client.DivMany()) 73 { 74 await call.RequestStream.WriteAllAsync(divArgsList); 75 Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToListAsync())); 76 } 77 } 78 DependendRequestsExample(Math.MathClient client)79 public static async Task DependendRequestsExample(Math.MathClient client) 80 { 81 var numbers = new List<Num> 82 { 83 new Num { Num_ = 1 }, 84 new Num { Num_ = 2 }, 85 new Num { Num_ = 3 } 86 }; 87 88 Num sum; 89 using (var sumCall = client.Sum()) 90 { 91 await sumCall.RequestStream.WriteAllAsync(numbers); 92 sum = await sumCall.ResponseAsync; 93 } 94 95 DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count }); 96 Console.WriteLine("Avg Result: " + result); 97 } 98 99 /// <summary> 100 /// Shows how to handle a call ending with non-OK status. 101 /// </summary> HandleErrorExample(Math.MathClient client)102 public static async Task HandleErrorExample(Math.MathClient client) 103 { 104 try 105 { 106 DivReply result = await client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }); 107 } 108 catch (RpcException ex) 109 { 110 Console.WriteLine(string.Format("RPC ended with status {0}", ex.Status)); 111 } 112 } 113 114 /// <summary> 115 /// Shows how to send request headers and how to access response headers 116 /// and response trailers. 117 /// </summary> MetadataExample(Math.MathClient client)118 public static async Task MetadataExample(Math.MathClient client) 119 { 120 var requestHeaders = new Metadata 121 { 122 { "custom-header", "custom-value" } 123 }; 124 125 var call = client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }, requestHeaders); 126 127 // Get response headers 128 Metadata responseHeaders = await call.ResponseHeadersAsync; 129 130 var result = await call; 131 132 // Get response trailers after the call has finished. 133 Metadata responseTrailers = call.GetTrailers(); 134 } 135 } 136 } 137