Create a gRPC Server Application With .NET Core

Introduction

 
In this post, we will learn about gRPC and will create a gRPC server application using Visual studio and .Net Core 3.1.
 

About gRPC

 
gRPC is a language-agnostic, high-performance Remote Procedure Call (RPC) framework.
 
gRPC Features
  •  High performance, contract-based API development.
  • Supports client, server, uni, and bi-directional streaming calls.
  • Binary communication support for all ecosystems(Java, .Net, etc) 
gRPC Pros
  • gRPC mainly supports binary-based, which benefits from less data and faster network transfer.
  • Contracts based development focus on a particular unit of business.
  • Good for middleware applications like service to service communication. 
gRPC Cons
  • Requires HTTP2 protocol, still not adopted by many existing applications.
  • Most browsers do not support Http2 and binary parsing.

gRPC Server Application

 
To create gRPC application using visual studio, we need below pre-requisite needs to be installed:
  • Visual studio 2019
  • .NET Core 3 or 3.1 
Step 1
 
First, we need to create a New gRPC application project type in Visual Studio:
 
Create gRPC Server Application With .Net Core
 
Step 2
 
We can choose additional options to configure the application and notice the .NET core version 3.1.3.
 
Create gRPC Server Application With .Net Core
 
Step 3
 
Once the project got created, we need to add the below NuGet packages,
 
Grpc.AspNetCore
Google.Protobuf
 
Create gRPC Server Application With .Net Core
 
Create gRPC Server Application With .Net Core
 
Step 4
 
Create a proto folder and add the proto file:
 
About ProtoBuff:
 
Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler.
 
We need to specify information in .proto files to be structured by defining protocol buffer message types.
 
In the below Employee.proto file, we have added the below elements,
  • Included EmployeeService as "Service" that's mandatory for gRPC communication.
  • Added two protocol buffer messages, "EmplyeeModel", "ResponseMessage" which is a small logical record of information, containing a series of name-value pairs. 
Create gRPC Server Application With .Net Core
 
We need to hint the compiler that this proto file needs to be processed with the type of application (client or server).
 
Right-click the proto file and change the “Build Action” and “gRPC Stub Class” properties. This will be an important step for the proto file to build and run the auto-generated files.
 
Create gRPC Server Application With .Net Core
 
After build, check the project properties to see if the reference has updated correctly or not.
 
Create gRPC Server Application With .Net Core
 
Step 5
 
It's time to verify that the proto files are compiled correctly and that respective C# classes are created.
 
We need to check if the Employee.cs and EmployeeGrpc files were generated after the build.
 
Create gRPC Server Application With .Net Core
 
Step 6
 
Created a "GetEmployeeService" service which is inherited from Employeeservice.EmployeeServiceBase.
 
Added the "GetAllEmployee" procedure which we will call this procedure from gRPC client.
 
Also injected the "EmployeeRepository" and called "GetAllEmployee" method. We are using a repository pattern here so that it will easily be replaced by the Mock repository.
 
Create gRPC Server Application With .Net Core
 
Step 7
 
For simplicity, I implemented the "IEmployeeRepository" with a Mock repository. We can replace this mock implementation with the database later.
 
Create gRPC Server Application With .Net Core
 
Step 8
 
Now run the project and make sure the gRPC server application run correctly. Our gRPC server application is running on URLs "https://localhost:5001" and "http:localhost:5000".
 
These URL's are endpoints for gRPC client applications.
 
Create gRPC Server Application With .Net Core
 

Summary

 
In this post, we learned about gRPC and created a gRPC Server application.


Similar Articles