gRPC Introduction and Implementation using .NET Core 6

Jaydeep Patil
6 min readAug 28, 2022

--

We are going to discuss the gRPC and its implementation using .NET Core 6

Agenda

Introduction of gRPC

Different scenarios in which we use gRPC

Pros and Cons of gRPC

Implementation of gRPC

Prerequisites

Visual Studio 2022

Basic Knowledge of C#

Introduction

  • gRPC stands for google Remote Procedure Calls
  • gRPC is a modern open-source high-performance Remote Procedure Call (RPC) framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking, and authentication. It is also applicable in the last mile of distributed computing to connect devices, mobile applications, and browsers to backend services. — gRPC Page

Fig. From grpc.io

  • gRPC is the framework that is used to implement APIs using HTTP/2
  • Basically, gRPC uses the protobuf for serialization and HTTP2 protocol which provides lots more advantages than HTTP
  • gRPC clients and servers intercommunicate using a variety of environments and machines, It Also supports many languages like Java, C#, Go, Ruby and Python.
  • The Binary layer of gRPC will do all data operations like encoding and it also uses protobuf as an intermediator between client and server, improving performance.
  • It is also used for communication between multiple microservices efficiently

Different scenarios in which we use gRPC

  • When we use microservice architecture and we use that for internal communication from one or more servers
  • It is also useful when performance is on high priority and low latency
  • When we require duplex communication between services with different types of data

Pros and Cons of gRPC

Pros

  • High Performance — faster than REST and SOAP
  • Lightweight Message — gRPC Message is more lightweight than other types like JSON
  • High Efficiency than other type
  • Duplex data streaming

Cons

  • Limited Browser Support
  • It uses Binary Data due that it’s not easily readable like JSON and XML

Implementation of gRPC

Step 1

Create a new gRPC project

Step 2

Configure the project

Step 3

Provide additional information

Step 4

Project Structure

Here you will see the default project structure with greet proto and Greeter Service

  • Protobuf is Interface Definition Language (IDL) for gRPC and uses to store data and contracts between clients and servers
  • Line No 1 to 5 as you see there we declare types of protobuf syntax, namespace, and package
  • Line No 7 to 11 there is a Unary Service Definition which takes a single request and the server sends back the response and it works as a function
  • There are also many service definitions like Server Streaming, Client Streaming, and Bidirectional streaming RPCs. If you want to learn more about then read the grpc.io document
  • Later on, there is a request and response function in order

This is the Greeter Service which inherits from Greeter.GreeterBase and inside that we inject ILogger and there is one method that takes the message sent by the client and sends it back a response

(Note: Build Application whenever you add a new proto and Service)

Also, make sure the proto property is configured properly as shown below

Here we can see Build Action Protobuf compiler and gRPC Stub Classes are Server only

Let’s create a Client Console Application

Step 1

Create a new console application

Step 2

configure your project

Step 3

provide additional information

Step 4

Copy greet proto file from the server and change the gRPC Stub Classes to Client only and build it

Step 5

Add client code inside the Program file

  • Here we create a channel after configuring the server’s URL and create a channel
  • Later on, call the method after passing a parameter to the server and print the response message inside the console

Step 6

Finally, run your code after configuring your both project as startup projects in proper order

This is the final output

Now we are going to add our new proto and service related to the product application in that we pass the product id from the client and the server will send the particular product details back to the client

Step 1

Create a new product proto file and change properties to Protobuf compiler and Servers only after that build the project

Here you can see, that we define service which takes product id as a parameter and send product details to the client

Step 2

Next, create a Product Service

Here you can see we create Product Service which inherits from Product.ProductBase and after that, we inject ILogger of type Product Service and create one method and inside that whatever product details client wants that we check and return as a response corresponding to the particular product which is sent by the client

Step 3

Map the Product Service inside the Program class

Step 4

Build Server Project

Step 5

Copy the Product protobuf file inside the client and change the properties to the client only

Step 6

Next, add client functionality inside the Program class

Here we create a channel after configuring the server’s URL and then create a model object with a user id and pass it to the method which gets the product details as a response. Finally, we just print the product details in the console

Step 7

Build and run your both projects after setting up as a startup project

So, this is the output of our service.

In this section, we just understand the basic working of gRPC with Product Application. But, in real-time scenarios, there are many ways to implement that like using Background service and some different gRPC streaming techniques that we discussed in upcoming articles

GitHub URL

https://github.com/Jaydeep-007/GrpcService/tree/master

Conclusion

In this article, we discussed gRPC and the pros and cons of gRPC. Also, the scenarios in which gRPC plays a vital role and step-by-step implementation using .NET Core 6

Happy Coding!

--

--

Jaydeep Patil

Full Stack Developer | .Net Core API | Angular | SQL Server | Docker | Azure | Python