What is a GRPC service ?
It is a remote procedure protocol call(RPC) framework , conceptually similar to WCF service where GRPC provides Contract-first API development. It supports client server architecture and bi direction streaming calls.
In this post lets cover how to get started with a Hello world application in creating a GRPC service. We will create
- A service to run GRPC service
- Client sending message and receiving a reply
Click on Visual studio ->new project and search for GRPC which results in grpc service project as below. Click on next
Enter the project name and select the folder. Clicking on create will create a project with default files
These are the default files displayed
The protos folder has greet.proto file. This is similar to contract in wcf. This proto file will be the mediator for both client and server. This proto file defines the methods and its parameters exposed . Have a look at the file
Lets look at the file in detail:
ln 3: The namespace where the service is available . Here it is “GrpcService2”
ln5 : The file name of proto is mentioned as package
ln8 : Contains name of the service and its contracts
Name -> Greeter
Contract -> Currently only one .SayHello is the name of the contract. Takes HelloRequest object as input. Returns HelloReply as response. The definition of HelloRequest and HelloReply are ln 14 and ln 19 respectively.
ln14 : HelloRequest object has string name property. The number 1 represents order of serialization . currently it is single property and 1 doesnt make much difference
ln19 : HelloReply object also has a string property with name message.
If you notice the proto file is just a definition of how the contract should be. It doesn't talk about the implementation of the services .
GreeterService is the service which implements the Greeter proto file
The service is inherited from the Greeterbase base class and implements the SayHello contract with HelloRequest as parameter and HelloReply as return . This service runs on Asp.net core by default.
The method takes a string as input , appends Hello and returns the string.
Note: if you need to create a new proto file remember to edit .cs proj file add the proto file . As greet.proto was added by default the file name was also added by default
Unless you are unluck the project should build and when you run, should see the window as below
As you see the service runs on https://localhost:5001 and awaits for a client to connect. Keep it running, as client is on the way
GRPC Client
Create a console application with .net core ( No screenshot provided ) and add the below nugget packages
Also copy only the proto file from server and add it in the client application under proto folder
Edit the .csproj file and add the below statement
Add the blow lines of code in program.cs file
Ln12 should contain the address where the service is running
We are passing “GreeterClient” as parameter and the response is append with Hello and returned
The client has made a call to the GRPC service and returned with a result. If you were following along hope you were also successful in establishing a connection, sending a request and receiving a response.
Thanks for reading. Please share your comments/feedback , also a clap if you do like it. Happy programming