Skip to content

Commit 722c878

Browse files
committed
Add Dapr’s gRPC proxying capability example
1 parent 0873c5e commit 722c878

File tree

7 files changed

+156
-2
lines changed

7 files changed

+156
-2
lines changed

examples/AspNetCore/GrpcServiceSample/GrpcServiceSample.csproj

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

88
<ItemGroup>
9-
<Protobuf Include="Protos\*.proto" ProtoRoot="Protos" GrpcServices="None" />
9+
<Protobuf Include="Protos\*.proto" ProtoRoot="Protos" GrpcServices="Server" />
1010
</ItemGroup>
1111

1212
<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())
31+
{
32+
await responseStream.WriteAsync(new HelloReply() { Message = "Hello " + message.Name });
33+
}
34+
}
35+
}

examples/AspNetCore/GrpcServiceSample/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5454
app.UseEndpoints(endpoints =>
5555
{
5656
endpoints.MapGrpcService<BankingService>();
57+
endpoints.MapGrpcService<GreeterService>();
5758

5859
endpoints.MapGet("/", async context =>
5960
{
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/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Program
2121
{
2222
private static readonly Example[] Examples = new Example[]
2323
{
24+
new InvokeServiceGrpcByDaprProxyExample(),
2425
new InvokeServiceGrpcExample(),
2526
new InvokeServiceHttpExample(),
2627
new InvokeServiceHttpClientExample(),

examples/Client/ServiceInvocation/ServiceInvocation.csproj

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

1010
<ItemGroup>
11-
<Protobuf Include="..\..\AspNetCore\GrpcServiceSample\Protos\*.proto" ProtoRoot="..\..\AspNetCore\GrpcServiceSample\Protos\" GrpcServices="None" />
11+
<Protobuf Include="..\..\AspNetCore\GrpcServiceSample\Protos\*.proto" ProtoRoot="..\..\AspNetCore\GrpcServiceSample\Protos\" GrpcServices="Client" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

0 commit comments

Comments
 (0)