In today’s newsletter we are going to discuss
What is gRPC
gRPC vs REST
Benefits of using gRPC
.NET 6 Implementation
When to use gRPC in .NET
What is gRPC
RPC : A technique to construct distributed client-server based applications , it is like you make an local call but it invokes something at that distributed machine and sends you a response
gRPC is a modern open source high performance Remote Procedure Call (RPC) framework , g stands for Google.
gRPC vs REST
Let’s see some differences b/w these two protocols:
Protocol : gRPC uses HTTP/2 and REST uses HTTP/1.1
Data Format : gRPC uses protocol buffers and REST uses JSON/XML
Streaming : gRPC is bi directional while REST works on request/response model
Performance : gRPC performance is better than REST
Benefits of gRPC
It is faster because it uses protocol buffers
It supports multiple languages making it a tool of choice for polyglot languages
Polyglot : using multiple programming languages to serve different tasks
it supports four types of communication (Unary , server streaming , client streaming and bi directional streaming.
Implementation in .NET 6.0
STEP 1 : Create a project of type , 'ASP .NET Core gRPC Service’ . This will act as server.
STEP 2 : Within same solution add another project , for example a console application. This will act as client
Our solution would look like this:
Understanding Server Side
Let’s see Protos folder which contain a proto file whose code looks like this :
This file defines how are you going to communicate , ignore first three lines.
HelloReply : In simple words it is a DTO written in proto buffer style , which says that the response that server will send to client will contain these properties.
HelloRequest : When a client will send request to server the request will contain properties mentioned in this message.
Ordering in reply and request is giving instruction that properties would be in this order. You can change these names as well.
service Greeter : This part is responsible for rpc and it takes a request in parameter and returns the response.
Let’s see Services folder which contain a .cs file whose code looks like this :
Request will contains the properties which we defined in our proto.
Greeter.GreeterBase : it is an auto created class and its name would be what you define in your proto (for our example service Greeter)
Last part is how the cs project file looks like for Server:
Understanding Client Side
Client side which is a console application is empty right now with only one file of Program.cs , install these three Nuget Packages which are necessary to communicate with server.
Next step is copy the proto buffer file from server and paste it on client side by creating a new folder of Protos.
Change namespace in proto file and replace ItemGroup
of client with this code
We are telling that this proto is for client purposes.
Last important thing is just click on proto files in client and server side and open its properties and perform these two actions
—> Set
BuildAction to Protobuf compiler
—> Set gRPC Stub Classes to Server Only for Server and Client Only for Client
Let’s add client side code now to communicate with server
In ForAddress
we have added server address which is address of where our server app is running , one thing is important to keep in mind that we have not added any reference b/w client and server project.
That is all you need to do now set both projects to run at start by right clicking on solution —> Configure start up project —> Set multiple
When to use gRPC in .NET ?
1) Microservices Architecture: gRPC with its small, fast, and efficient binary size is ideal for microservices.
2) Real-Time Applications: It’s a perfect fit for real-time and streaming applications due to its support for bidirectional streaming.
3) Polyglot Environments: If your application needs to communicate with services written in multiple languages.
You can download demo code at GitHub Repository
Whenever you’re ready , there are 4 ways I can help you
Promote yourself to 5900+ subscribers by sponsoring my Newsletter
Become a Patron and get access to 140+ .NET Questions and Answers
Get FREE eBook from Gumroad that contains 30 .NET Tips (2500+ downloads)
Book a one hour 1:1 Session and learn how I went from 0-31K on LinkedIn in 8 months , Book here
Special Offers 📢
Ultimate ASP.NET Core Web API Second Edition - Premium Package
10% off with discount code: 9s6nuez