add more examples and improve existing ones

pull/6526/head
Jan Tattermusch 9 years ago
parent e28415bdec
commit d6a8397bcc
  1. 2
      src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
  2. 38
      src/csharp/Grpc.Examples/MathExamples.cs
  3. 29
      src/csharp/Grpc.Examples/MathServiceImpl.cs

@ -92,7 +92,7 @@ namespace Math.Tests
public void DivByZero()
{
var ex = Assert.Throws<RpcException>(() => client.Div(new DivArgs { Dividend = 0, Divisor = 0 }));
Assert.AreEqual(StatusCode.Unknown, ex.Status.StatusCode);
Assert.AreEqual(StatusCode.InvalidArgument, ex.Status.StatusCode);
}
[Test]

@ -32,6 +32,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Utils;
namespace Math
@ -109,5 +110,42 @@ namespace Math
DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count });
Console.WriteLine("Avg Result: " + result);
}
/// <summary>
/// Shows how to handle a call ending with non-OK status.
/// </summary>
public static async Task HandleErrorExample(Math.MathClient client)
{
try
{
DivReply result = await client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 });
}
catch (RpcException ex)
{
Console.WriteLine(string.Format("RPC ended with status {0}", ex.Status));
}
}
/// <summary>
/// Shows how to send request headers and how to access response headers
/// and response trailers.
/// </summary>
public static async Task MetadataExample(Math.MathClient client)
{
var requestHeaders = new Metadata
{
{ "custom-header", "custom-value" }
};
var call = client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }, requestHeaders);
// Get response headers
Metadata responseHeaders = await call.ResponseHeadersAsync;
var result = await call;
// Get response trailers after the call has finished.
Metadata responseTrailers = call.GetTrailers();
}
}
}

@ -52,23 +52,15 @@ namespace Math
public override async Task Fib(FibArgs request, IServerStreamWriter<Num> responseStream, ServerCallContext context)
{
if (request.Limit <= 0)
{
// keep streaming the sequence until cancelled.
IEnumerator<Num> fibEnumerator = FibInternal(long.MaxValue).GetEnumerator();
while (!context.CancellationToken.IsCancellationRequested && fibEnumerator.MoveNext())
{
await responseStream.WriteAsync(fibEnumerator.Current);
await Task.Delay(100);
}
}
var limit = request.Limit > 0 ? request.Limit : long.MaxValue;
var fibEnumerator = FibInternal(limit).GetEnumerator();
if (request.Limit > 0)
// Keep streaming the sequence until the call is cancelled.
// Use CancellationToken from ServerCallContext to detect the cancellation.
while (!context.CancellationToken.IsCancellationRequested && fibEnumerator.MoveNext())
{
foreach (var num in FibInternal(request.Limit))
{
await responseStream.WriteAsync(num);
}
await responseStream.WriteAsync(fibEnumerator.Current);
await Task.Delay(100);
}
}
@ -89,6 +81,13 @@ namespace Math
static DivReply DivInternal(DivArgs args)
{
if (args.Divisor == 0)
{
// One can finish the RPC with non-ok status by throwing RpcException instance.
// Alternatively, resulting status can be set using ServerCallContext.Status
throw new RpcException(new Status(StatusCode.InvalidArgument, "Division by zero"));
}
long quotient = args.Dividend / args.Divisor;
long remainder = args.Dividend % args.Divisor;
return new DivReply { Quotient = quotient, Remainder = remainder };

Loading…
Cancel
Save