3. In a class library:
• Create Folder “protos” – for server interface and
types descripted files.
• Create Folder “gen” – for generated by grps tools
code.
• Add new item to project – “generate_protos.bat”
file
• Remove useless Class1 file
4. Add such gRPC nuget references:
• Grpc.Core (v 0.14.0)
• Grpc.Tools (v 0.14.0)
• Google.protobuf.custom (v 3.0.0 beta-2
6. Create proto file
• In a “protos” folder create “helloworld.proto” file.
• Add Server interface and types code to proto file
(next slide)
7. Proto file content:
syntax = "proto3";
option java_multiple_files = true;
option java_package =
"io.grpc.examples.helloworld";
option java_outer_classname =
"HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns
(HelloReply) {}
}
// The request message containing the user's
name.
message HelloRequest {
string name = 1;
}
// The response message containing the
greetings
message HelloReply {
string message = 1;
}
8. Create bat file for generating
stub code from proto file
• Add to project file “generate_protos.bat” file
• This bat will run gRPC code generating tools with
argument proto file
9. Generate_protos.bat content
@rem Generate the C# code for .proto files
setlocal
@rem enter this directory
cd /d %~dp0
@rem set TOOLS_PATH=..packagesGrpc.Tools.0.14.0toolswindows_x86
set TOOLS_PATH=..packagesGrpc.Tools.0.14.0toolswindows_x86
%TOOLS_PATH%protoc.exe -I protos --csharp_out gen protos/helloworld.proto --grpc_out gen -
-plugin=protoc-gen-grpc=%TOOLS_PATH%grpc_csharp_plugin.exe
endlocal
10. Generating stub code:
• Run cmd, go to generate_protos.bat location
• Run generate_protos.bat
• Open “gen”, see newly generated files appeared
• Add new files to project
• Build Project in x86 configuration
11. Add Server project
• Add “GreeterServer” visual C# console application project.
• With nuget package add Grpc.Core (v 0.14.0) package.
• Reference “Greeter” project to GreeterServer project
• Add using Grpc.Core and Helloworld.
• Add GreeterImpl class to Server project code
class GreeterImpl : Greeter.IGreeter
{
// Server side handler of the SayHello RPC
public Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
}
}
12. Modify Program.cs
class Program
{
const int Port = 50051;
static void Main(string[] args)
{
Server server = new Server
{
Services = { Greeter.BindService(new GreeterImpl()) },
Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
};
server.Start();
Console.WriteLine("Greeter server listening on port " + Port);
Console.WriteLine("Press any key to stop the server...");
Console.ReadKey();
server.ShutdownAsync().Wait();
}
}
13. Add Client project
• Add “GreeterClient” visual C# console application project.
• With nuget package add Grpc.Core (v 0.14.0) package.
• Reference “Greeter” project to GreeterServer project
• Add using Grpc.Core and Helloworld.
14. Modify Program.cs
class Program
{
static void Main(string[] args)
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = Greeter.NewClient(channel);
String user = "you";
var reply = client.SayHello(new HelloRequest { Name = user });
Console.WriteLine("Greeting: " + reply.Message);
channel.ShutdownAsync().Wait();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
15. Build and Run
• Build entire solution in x86 configuration, see it is
building without errors.
• Run GreetingServer and GreetingClient
• See GreetingClient receives response.
17. Практичне завдання:
• Базуючись на попередньому Helloworld прикладі
• Створити клієнт серверну програму, яка
використовуватиме gRPC для комунікації
• Вид аплікації – це система для офіціанта в кафе, де
він записує замовлення, клієнтів і т.д.
• Для gRPC методів можна використовувати прості
типи (тільки string, як в прокладі )
• Ця практична використовуватиметься як база для
наступного практичного завдання