MathExamples.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #region Copyright notice and license
  2. // Copyright 2015-2016, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Threading.Tasks;
  34. using Grpc.Core;
  35. using Grpc.Core.Utils;
  36. namespace Math
  37. {
  38. public static class MathExamples
  39. {
  40. public static void DivExample(Math.MathClient client)
  41. {
  42. DivReply result = client.Div(new DivArgs { Dividend = 10, Divisor = 3 });
  43. Console.WriteLine("Div Result: " + result);
  44. }
  45. public static async Task DivAsyncExample(Math.MathClient client)
  46. {
  47. DivReply result = await client.DivAsync(new DivArgs { Dividend = 4, Divisor = 5 });
  48. Console.WriteLine("DivAsync Result: " + result);
  49. }
  50. public static async Task FibExample(Math.MathClient client)
  51. {
  52. using (var call = client.Fib(new FibArgs { Limit = 5 }))
  53. {
  54. List<Num> result = await call.ResponseStream.ToListAsync();
  55. Console.WriteLine("Fib Result: " + string.Join("|", result));
  56. }
  57. }
  58. public static async Task SumExample(Math.MathClient client)
  59. {
  60. var numbers = new List<Num>
  61. {
  62. new Num { Num_ = 1 },
  63. new Num { Num_ = 2 },
  64. new Num { Num_ = 3 }
  65. };
  66. using (var call = client.Sum())
  67. {
  68. await call.RequestStream.WriteAllAsync(numbers);
  69. Console.WriteLine("Sum Result: " + await call.ResponseAsync);
  70. }
  71. }
  72. public static async Task DivManyExample(Math.MathClient client)
  73. {
  74. var divArgsList = new List<DivArgs>
  75. {
  76. new DivArgs { Dividend = 10, Divisor = 3 },
  77. new DivArgs { Dividend = 100, Divisor = 21 },
  78. new DivArgs { Dividend = 7, Divisor = 2 }
  79. };
  80. using (var call = client.DivMany())
  81. {
  82. await call.RequestStream.WriteAllAsync(divArgsList);
  83. Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToListAsync()));
  84. }
  85. }
  86. public static async Task DependendRequestsExample(Math.MathClient client)
  87. {
  88. var numbers = new List<Num>
  89. {
  90. new Num { Num_ = 1 },
  91. new Num { Num_ = 2 },
  92. new Num { Num_ = 3 }
  93. };
  94. Num sum;
  95. using (var sumCall = client.Sum())
  96. {
  97. await sumCall.RequestStream.WriteAllAsync(numbers);
  98. sum = await sumCall.ResponseAsync;
  99. }
  100. DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count });
  101. Console.WriteLine("Avg Result: " + result);
  102. }
  103. /// <summary>
  104. /// Shows how to handle a call ending with non-OK status.
  105. /// </summary>
  106. public static async Task HandleErrorExample(Math.MathClient client)
  107. {
  108. try
  109. {
  110. DivReply result = await client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 });
  111. }
  112. catch (RpcException ex)
  113. {
  114. Console.WriteLine(string.Format("RPC ended with status {0}", ex.Status));
  115. }
  116. }
  117. /// <summary>
  118. /// Shows how to send request headers and how to access response headers
  119. /// and response trailers.
  120. /// </summary>
  121. public static async Task MetadataExample(Math.MathClient client)
  122. {
  123. var requestHeaders = new Metadata
  124. {
  125. { "custom-header", "custom-value" }
  126. };
  127. var call = client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }, requestHeaders);
  128. // Get response headers
  129. Metadata responseHeaders = await call.ResponseHeadersAsync;
  130. var result = await call;
  131. // Get response trailers after the call has finished.
  132. Metadata responseTrailers = call.GetTrailers();
  133. }
  134. }
  135. }