diff options
Diffstat (limited to 'src/csharp/Grpc.Examples/MathGrpc.cs')
-rw-r--r-- | src/csharp/Grpc.Examples/MathGrpc.cs | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/src/csharp/Grpc.Examples/MathGrpc.cs b/src/csharp/Grpc.Examples/MathGrpc.cs index 33a9ca9287..24e6a1de8e 100644 --- a/src/csharp/Grpc.Examples/MathGrpc.cs +++ b/src/csharp/Grpc.Examples/MathGrpc.cs @@ -45,6 +45,8 @@ namespace math /// </summary> public class MathGrpc { + static readonly string ServiceName = "/math.Math"; + static readonly Marshaller<DivArgs> DivArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivArgs.ParseFrom); static readonly Marshaller<DivReply> DivReplyMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivReply.ParseFrom); static readonly Marshaller<Num> NumMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), Num.ParseFrom); @@ -52,25 +54,25 @@ namespace math static readonly Method<DivArgs, DivReply> DivMethod = new Method<DivArgs, DivReply>( MethodType.Unary, - "/math.Math/Div", + "Div", DivArgsMarshaller, DivReplyMarshaller); static readonly Method<FibArgs, Num> FibMethod = new Method<FibArgs, Num>( MethodType.ServerStreaming, - "/math.Math/Fib", + "Fib", FibArgsMarshaller, NumMarshaller); static readonly Method<Num, Num> SumMethod = new Method<Num, Num>( MethodType.ClientStreaming, - "/math.Math/Sum", + "Sum", NumMarshaller, NumMarshaller); static readonly Method<DivArgs, DivReply> DivManyMethod = new Method<DivArgs, DivReply>( MethodType.DuplexStreaming, - "/math.Math/DivMany", + "DivMany", DivArgsMarshaller, DivReplyMarshaller); @@ -87,42 +89,43 @@ namespace math IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver, CancellationToken token = default(CancellationToken)); } - public class MathServiceClientStub : IMathServiceClient + public class MathServiceClientStub : AbstractStub<MathServiceClientStub, StubConfiguration>, IMathServiceClient { - readonly Channel channel; + public MathServiceClientStub(Channel channel) : this(channel, StubConfiguration.Default) + { + } - public MathServiceClientStub(Channel channel) + public MathServiceClientStub(Channel channel, StubConfiguration config) : base(channel, config) { - this.channel = channel; } public DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)) { - var call = new Grpc.Core.Call<DivArgs, DivReply>(DivMethod, channel); + var call = CreateCall(ServiceName, DivMethod); return Calls.BlockingUnaryCall(call, request, token); } public Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)) { - var call = new Grpc.Core.Call<DivArgs, DivReply>(DivMethod, channel); + var call = CreateCall(ServiceName, DivMethod); return Calls.AsyncUnaryCall(call, request, token); } public void Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken)) { - var call = new Grpc.Core.Call<FibArgs, Num>(FibMethod, channel); + var call = CreateCall(ServiceName, FibMethod); Calls.AsyncServerStreamingCall(call, request, responseObserver, token); } public ClientStreamingAsyncResult<Num, Num> Sum(CancellationToken token = default(CancellationToken)) { - var call = new Grpc.Core.Call<Num, Num>(SumMethod, channel); + var call = CreateCall(ServiceName, SumMethod); return Calls.AsyncClientStreamingCall(call, token); } public IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver, CancellationToken token = default(CancellationToken)) { - var call = new Grpc.Core.Call<DivArgs, DivReply>(DivManyMethod, channel); + var call = CreateCall(ServiceName, DivManyMethod); return Calls.DuplexStreamingCall(call, responseObserver, token); } } @@ -141,7 +144,7 @@ namespace math public static ServerServiceDefinition BindService(IMathService serviceImpl) { - return ServerServiceDefinition.CreateBuilder("/math.Math/") + return ServerServiceDefinition.CreateBuilder(ServiceName) .AddMethod(DivMethod, serviceImpl.Div) .AddMethod(FibMethod, serviceImpl.Fib) .AddMethod(SumMethod, serviceImpl.Sum) @@ -152,5 +155,10 @@ namespace math { return new MathServiceClientStub(channel); } + + public static IMathServiceClient NewStub(Channel channel, StubConfiguration config) + { + return new MathServiceClientStub(channel, config); + } } } |