Skip to content

Instantly share code, notes, and snippets.

@scionwest
Last active May 9, 2017 23:07
Show Gist options
  • Save scionwest/ca07adba0e2fcea681bbe21c711568a3 to your computer and use it in GitHub Desktop.
Save scionwest/ca07adba0e2fcea681bbe21c711568a3 to your computer and use it in GitHub Desktop.
// gRPC channel
var channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
// Protobuf service
var client = new Messenger.MessengerClient(channel);
// Protobuf model
var request = new HelloRequest { Name = "You" };
var reply = await client.SayHelloAsync(request);
channel.ShutdownAsync().Wait();
syntax = "proto3";
package AComms.Services;
// Client service definitions
service Messenger {
// Send a greeting message
rpc SayHello(HelloRequest) returns (HelloReply) {}
}
// The request message
message HelloRequest {
string Name = 1;
}
message HelloReply {
string message = 1;
}
namespace AComms
{
internal class MessengerService : Messenger.MessengerBase
{
// Listen for SayHello requests from client and react.
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
}
}
class Program
{
static void Main(string[] args)
{
var server = new Server
{
// Protobuf service
Services = { Messenger.BindService(new MessengerService()) },
Ports = { new ServerPort("localhost", 50051, ServerCredentials.Insecure) }
};
server.Start();
Console.WriteLine("Server running\nPress any key to stop.");
Console.ReadKey();
server.ShutdownAsync().Wait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment