Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | // Copyright 2015-2016 gRPC authors. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 3 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 7 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 15 | #endregion |
| 16 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 17 | using System; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 18 | using System.Collections.Generic; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 19 | using System.Threading.Tasks; |
Jan Tattermusch | d6a8397 | 2016-05-10 16:38:23 -0700 | [diff] [blame] | 20 | using Grpc.Core; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 21 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 22 | |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 23 | namespace Math |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 24 | { |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 25 | public static class MathExamples |
| 26 | { |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 27 | public static void DivExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 28 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 29 | DivReply result = client.Div(new DivArgs { Dividend = 10, Divisor = 3 }); |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 30 | Console.WriteLine("Div Result: " + result); |
| 31 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 32 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 33 | public static async Task DivAsyncExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 34 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 35 | DivReply result = await client.DivAsync(new DivArgs { Dividend = 4, Divisor = 5 }); |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 36 | Console.WriteLine("DivAsync Result: " + result); |
| 37 | } |
| 38 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 39 | public static async Task FibExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 40 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 41 | using (var call = client.Fib(new FibArgs { Limit = 5 })) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 42 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 43 | List<Num> result = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 44 | Console.WriteLine("Fib Result: " + string.Join("|", result)); |
| 45 | } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 46 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 47 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 48 | public static async Task SumExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 49 | { |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 50 | var numbers = new List<Num> |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 51 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 52 | new Num { Num_ = 1 }, |
| 53 | new Num { Num_ = 2 }, |
| 54 | new Num { Num_ = 3 } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 55 | }; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 56 | |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 57 | using (var call = client.Sum()) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 58 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 59 | await call.RequestStream.WriteAllAsync(numbers); |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 60 | Console.WriteLine("Sum Result: " + await call.ResponseAsync); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 61 | } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 62 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 63 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 64 | public static async Task DivManyExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 65 | { |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 66 | var divArgsList = new List<DivArgs> |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 67 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 68 | new DivArgs { Dividend = 10, Divisor = 3 }, |
| 69 | new DivArgs { Dividend = 100, Divisor = 21 }, |
| 70 | new DivArgs { Dividend = 7, Divisor = 2 } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 71 | }; |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 72 | using (var call = client.DivMany()) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 73 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 74 | await call.RequestStream.WriteAllAsync(divArgsList); |
| 75 | Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToListAsync())); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 76 | } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 77 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 78 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 79 | public static async Task DependendRequestsExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 80 | { |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 81 | var numbers = new List<Num> |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 82 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 83 | new Num { Num_ = 1 }, |
| 84 | new Num { Num_ = 2 }, |
| 85 | new Num { Num_ = 3 } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 86 | }; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 87 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 88 | Num sum; |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 89 | using (var sumCall = client.Sum()) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 90 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 91 | await sumCall.RequestStream.WriteAllAsync(numbers); |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 92 | sum = await sumCall.ResponseAsync; |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 93 | } |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 94 | |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 95 | DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count }); |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 96 | Console.WriteLine("Avg Result: " + result); |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 97 | } |
Jan Tattermusch | d6a8397 | 2016-05-10 16:38:23 -0700 | [diff] [blame] | 98 | |
| 99 | /// <summary> |
| 100 | /// Shows how to handle a call ending with non-OK status. |
| 101 | /// </summary> |
| 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> |
| 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 | } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 135 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 136 | } |