|
| 1 | +// ------------------------------------------------------------------------ |
| 2 | +// Copyright 2021 The Dapr Authors |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | +// ------------------------------------------------------------------------ |
| 13 | + |
| 14 | +using System; |
| 15 | +using System.Threading; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using Dapr.Client; |
| 18 | +using Grpc.Net.Client; |
| 19 | +using GrpcServiceSample.Generated; |
| 20 | + |
| 21 | +namespace Samples.Client |
| 22 | +{ |
| 23 | + public class InvokeServiceGrpcByDaprProxyExample : Example |
| 24 | + { |
| 25 | + public override string DisplayName => "Invoking a existing proto-based gRPC service with DaprClient"; |
| 26 | + public override async Task RunAsync(CancellationToken cancellationToken) |
| 27 | + { |
| 28 | + Console.WriteLine("** use sdk created client **"); |
| 29 | + await UseSdkCreateClient(cancellationToken); |
| 30 | + Console.WriteLine("** use sdk created client completed **"); |
| 31 | + |
| 32 | + Console.WriteLine("** use grpc client **"); |
| 33 | + await UseSdkCreateClient(cancellationToken); |
| 34 | + Console.WriteLine("** use grpc client completed **"); |
| 35 | + } |
| 36 | + |
| 37 | + private async Task UseSdkCreateClient(CancellationToken cancellationToken) |
| 38 | + { |
| 39 | + var invoker = DaprClient.CreateInvocationInvoker("grpcsample"); |
| 40 | + var client = new Greeter.GreeterClient(invoker); |
| 41 | + |
| 42 | + await InvokeExample(client, cancellationToken); |
| 43 | + } |
| 44 | + private async Task UseGrpcClientOnly(CancellationToken cancellationToken) |
| 45 | + { |
| 46 | + var daprEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT"); |
| 47 | + if (string.IsNullOrEmpty(daprEndpoint)) |
| 48 | + { |
| 49 | + throw new InvalidOperationException("Dapr endpoint not set"); |
| 50 | + } |
| 51 | + var channel = GrpcChannel.ForAddress(daprEndpoint); |
| 52 | + var client = new Greeter.GreeterClient(channel); |
| 53 | + |
| 54 | + await InvokeExample(client, cancellationToken); |
| 55 | + } |
| 56 | + private static async Task InvokeExample(Greeter.GreeterClient client, CancellationToken cancellationToken) |
| 57 | + { |
| 58 | + |
| 59 | + Console.WriteLine("Invoking grpc SayHello"); |
| 60 | + var helloRequest = new HelloRequest { Name = "a request through dapr" }; |
| 61 | + var helloReply = await client.SayHelloAsync(helloRequest, cancellationToken: cancellationToken); |
| 62 | + Console.WriteLine("Returned: message:{0}", helloReply.Message); |
| 63 | + Console.WriteLine("Completed grpc SayHello"); |
| 64 | + |
| 65 | + Console.WriteLine("Invoking grpc Bi-directional streaming SayHelloStream"); |
| 66 | + var helloStream = client.SayHelloStream(cancellationToken: cancellationToken); |
| 67 | + for (int i = 0; i < 5; i++) |
| 68 | + { |
| 69 | + var helloStreamRequest = new HelloRequest { Name = $"a request through dapr {i}" }; |
| 70 | + await helloStream.RequestStream.WriteAsync(helloStreamRequest); |
| 71 | + if (!await helloStream.ResponseStream.MoveNext(cancellationToken)) |
| 72 | + { |
| 73 | + break; |
| 74 | + } |
| 75 | + Console.WriteLine("Returned: message:{0}", helloStream.ResponseStream.Current.Message); |
| 76 | + } |
| 77 | + Console.WriteLine("Completed grpc Bi-directional streaming SayHelloStream"); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + } |
| 82 | +} |
0 commit comments