Skip to content

Commit 4f42657

Browse files
ercJuLwatfordsuzyWhitWaldo
committed
Add Dapr’s gRPC proxying capability example (dapr#1525)
* Add Dapr’s gRPC proxying capability example Signed-off-by: ercJuL <[email protected]> * Update examples/AspNetCore/GrpcServiceSample/Services/GreeterService.cs Co-authored-by: Christopher Watford <[email protected]> Signed-off-by: ercJuL <[email protected]> --------- Signed-off-by: ercJuL <[email protected]> Co-authored-by: Christopher Watford <[email protected]> Co-authored-by: Whit Waldo <[email protected]>
1 parent e5f5966 commit 4f42657

File tree

5 files changed

+154
-2
lines changed

5 files changed

+154
-2
lines changed

examples/AspNetCore/GrpcServiceSample/GrpcServiceSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<Protobuf Include="Protos\*.proto" ProtoRoot="Protos" GrpcServices="None" />
8+
<Protobuf Include="Protos\*.proto" ProtoRoot="Protos" GrpcServices="Server" />
99
</ItemGroup>
1010

1111
<ItemGroup>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
syntax = "proto3";
15+
16+
option csharp_namespace = "GrpcServiceSample.Generated";
17+
18+
package helloworld;
19+
20+
// The greeting service definition.
21+
service Greeter {
22+
// Sends a greeting
23+
rpc SayHello (HelloRequest) returns (HelloReply) {}
24+
rpc SayHelloStream (stream HelloRequest) returns (stream HelloReply) {}
25+
}
26+
27+
// The request message containing the user's name.
28+
message HelloRequest {
29+
string name = 1;
30+
}
31+
32+
// The response message containing the greetings
33+
message HelloReply {
34+
string message = 1;
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.Threading.Tasks;
15+
using Grpc.Core;
16+
using GrpcServiceSample.Generated;
17+
18+
namespace GrpcServiceSample.Services;
19+
20+
public class GreeterService: Greeter.GreeterBase
21+
{
22+
public override async Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
23+
{
24+
await Task.Delay(1);
25+
return new HelloReply { Message = "Hello " + request.Name };
26+
}
27+
28+
public override async Task SayHelloStream(IAsyncStreamReader<HelloRequest> requestStream, IServerStreamWriter<HelloReply> responseStream, ServerCallContext context)
29+
{
30+
await foreach (var message in requestStream.ReadAllAsync(context.CancellationToken))
31+
{
32+
await responseStream.WriteAsync(new HelloReply() { Message = "Hello " + message.Name });
33+
}
34+
}
35+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

examples/Client/ServiceInvocation/ServiceInvocation.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<Protobuf Include="..\..\AspNetCore\GrpcServiceSample\Protos\*.proto" ProtoRoot="..\..\AspNetCore\GrpcServiceSample\Protos\" GrpcServices="None" />
10+
<Protobuf Include="..\..\AspNetCore\GrpcServiceSample\Protos\*.proto" ProtoRoot="..\..\AspNetCore\GrpcServiceSample\Protos\" GrpcServices="Client" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

0 commit comments

Comments
 (0)