MathExamples.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. using System;
  17. using System.Collections.Generic;
  18. using System.Threading.Tasks;
  19. using Grpc.Core;
  20. using Grpc.Core.Utils;
  21. namespace Math
  22. {
  23. public static class MathExamples
  24. {
  25. public static void DivExample(Math.MathClient client)
  26. {
  27. DivReply result = client.Div(new DivArgs { Dividend = 10, Divisor = 3 });
  28. Console.WriteLine("Div Result: " + result);
  29. }
  30. public static async Task DivAsyncExample(Math.MathClient client)
  31. {
  32. DivReply result = await client.DivAsync(new DivArgs { Dividend = 4, Divisor = 5 });
  33. Console.WriteLine("DivAsync Result: " + result);
  34. }
  35. public static async Task FibExample(Math.MathClient client)
  36. {
  37. using (var call = client.Fib(new FibArgs { Limit = 5 }))
  38. {
  39. List<Num> result = await call.ResponseStream.ToListAsync();
  40. Console.WriteLine("Fib Result: " + string.Join("|", result));
  41. }
  42. }
  43. public static async Task SumExample(Math.MathClient client)
  44. {
  45. var numbers = new List<Num>
  46. {
  47. new Num { Num_ = 1 },
  48. new Num { Num_ = 2 },
  49. new Num { Num_ = 3 }
  50. };
  51. using (var call = client.Sum())
  52. {
  53. await call.RequestStream.WriteAllAsync(numbers);
  54. Console.WriteLine("Sum Result: " + await call.ResponseAsync);
  55. }
  56. }
  57. public static async Task DivManyExample(Math.MathClient client)
  58. {
  59. var divArgsList = new List<DivArgs>
  60. {
  61. new DivArgs { Dividend = 10, Divisor = 3 },
  62. new DivArgs { Dividend = 100, Divisor = 21 },
  63. new DivArgs { Dividend = 7, Divisor = 2 }
  64. };
  65. using (var call = client.DivMany())
  66. {
  67. await call.RequestStream.WriteAllAsync(divArgsList);
  68. Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToListAsync()));
  69. }
  70. }
  71. public static async Task DependendRequestsExample(Math.MathClient client)
  72. {
  73. var numbers = new List<Num>
  74. {
  75. new Num { Num_ = 1 },
  76. new Num { Num_ = 2 },
  77. new Num { Num_ = 3 }
  78. };
  79. Num sum;
  80. using (var sumCall = client.Sum())
  81. {
  82. await sumCall.RequestStream.WriteAllAsync(numbers);
  83. sum = await sumCall.ResponseAsync;
  84. }
  85. DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count });
  86. Console.WriteLine("Avg Result: " + result);
  87. }
  88. /// <summary>
  89. /// Shows how to handle a call ending with non-OK status.
  90. /// </summary>
  91. public static async Task HandleErrorExample(Math.MathClient client)
  92. {
  93. try
  94. {
  95. DivReply result = await client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 });
  96. }
  97. catch (RpcException ex)
  98. {
  99. Console.WriteLine(string.Format("RPC ended with status {0}", ex.Status));
  100. }
  101. }
  102. /// <summary>
  103. /// Shows how to send request headers and how to access response headers
  104. /// and response trailers.
  105. /// </summary>
  106. public static async Task MetadataExample(Math.MathClient client)
  107. {
  108. var requestHeaders = new Metadata
  109. {
  110. { "custom-header", "custom-value" }
  111. };
  112. var call = client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }, requestHeaders);
  113. // Get response headers
  114. Metadata responseHeaders = await call.ResponseHeadersAsync;
  115. var result = await call;
  116. // Get response trailers after the call has finished.
  117. Metadata responseTrailers = call.GetTrailers();
  118. }
  119. }
  120. }