Skip to content

Commit

Permalink
consuming the streaming service
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinrfr committed Aug 3, 2020
1 parent f82ecbf commit cf7b1cd
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
In this project I'm doing some tests with this technology.

## Notes
- the way to return an "array of results" is through streaming, e.g. `rpc GetDataAsync(GetDataRequest) returns (stream GetDataReply);`
- the other way is to return an array inside a messege envelop, but this is not too performatic
- `proto files` = kind of interfaces to define the message format.
- `rpc SayHello (HelloRequest) returns (HelloReply);` = service definition
- each message should have a `order identifier`, e.g.:
Expand All @@ -11,6 +13,7 @@ In this project I'm doing some tests with this technology.
string message = 1;
}
```
- to create new services we should register them individually on Startup `endpoints.MapGrpcService<v1.StreamerService>();`
- [disabling TLS and making gRPC server run over http (insercure) due to OS compatibility (macOS)](https://docs.microsoft.com/pt-br/aspnet/core/grpc/troubleshoot?view=aspnetcore-3.1&source=docs#unable-to-start-aspnet-core-grpc-app-on-macos)
- [call insecure (http) gRPC services](https://docs.microsoft.com/pt-br/aspnet/core/grpc/troubleshoot?view=aspnetcore-3.1&source=docs#call-insecure-grpc-services-with-net-core-client)
Expand Down
36 changes: 30 additions & 6 deletions grpc-client-console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
using System;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Net.Client;
using grpc_server;
using grpc_server;
using grpc_server.v1;

namespace grpc_client_console
{
class Program
{
const string address = "http://localhost:5000";
static readonly GrpcChannel channel = GrpcChannel.ForAddress(address);

static async Task Main(string[] args)
{
Console.WriteLine("Waiting server warm up...");
await Task.Delay(TimeSpan.FromSeconds(5));

// This switch must be set before creating the GrpcChannel/HttpClient.
// Allowing gRPC run over insecure channels
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

await GreeterService();
Console.WriteLine($"Creating gRPC clients over channel '{address}'...");
await CallGreeterService();

await CallStreamerService();
}

private static async Task GreeterService()
private static async Task CallStreamerService()
{
Console.WriteLine($"Creating gRPC client over channel '{address}'...");
var channel = GrpcChannel.ForAddress(address);
Console.WriteLine("Creating streamer client..");
var streamerClient = new Streamer.StreamerClient(channel);

Console.WriteLine("Calling service...");
using var streamingReply = streamerClient.GetDataAsync(new GetDataRequest
{
Seed = 21
});

while (await streamingReply.ResponseStream.MoveNext())
{
var reply = streamingReply.ResponseStream.Current;
Console.WriteLine($"Message received {reply.Datetime} = {reply.Number}");
}
Console.WriteLine("All messages received");
}

private static async Task CallGreeterService()
{
Console.WriteLine("Creating greeter client..");
var greaterClient = new Greeter.GreeterClient(channel);

Console.WriteLine("Calling client...");
Expand Down
17 changes: 17 additions & 0 deletions grpc-client-console/Protos/v1/streamer.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

option csharp_namespace = "grpc_server.v1";

service Streamer {
rpc GetDataAsync(GetDataRequest) returns (stream GetDataReply);
}

message GetDataRequest
{
int32 seed = 1;
}

message GetDataReply {
int32 number = 1;
string datetime = 2;
}
3 changes: 3 additions & 0 deletions grpc-client-console/grpc-client-console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Protos\" />
<Folder Include="Protos\v1\" />
</ItemGroup>
<ItemGroup>
<None Remove="Protos\greet.proto" />
<None Remove="Protos\v1\streamer.proto" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
<Protobuf Include="Protos\v1\streamer.proto" GrpcServices="Client" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion grpc-server/Services/v1/StreamerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async override Task GetDataAsync(
{
await responseStream.WriteAsync(new GetDataReply
{
Number = random.Next(),
Number = random.Next(maxValue: 100),
Datetime = DateTime.UtcNow.ToString()
});
await Task.Delay(TimeSpan.FromSeconds(1));
Expand Down
1 change: 1 addition & 0 deletions grpc-server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<GreeterService>();
endpoints.MapGrpcService<v1.StreamerService>();

endpoints.MapGet("/", async context =>
{
Expand Down

0 comments on commit cf7b1cd

Please sign in to comment.