2nd Sep 2022

Enabling the C# with gRPC Services effectively

.NET

Written By, Amisha Panchal

Overview

Hello Techies, Today, we will see something interesting. In this blog, you will learn about gRPC services. But before understanding gRPC, first you have to understand “What is RPC?” RPC means Remote Procedure Call.

A RPC is a type of Client-Server Communication that uses a function call instead of a standard HTTP request. It utilizes IDL (Interface Definition Language) as a contract for executing functions and defining data types. Let’s get some deep ideas about gRPC.

Introduction

gRPC was created by Google in 2015 as an extension of the RPC framework to connect a variety of microservices built with different technologies.

Microservices are commonly used to run multiple services for various programming languages. They’ll also have a huge number of interpersonal interactions in the corporate world.

MicroService

gRPC is an open-source RPC (remote procedure call) framework that can be used to create scalable and faster APIs. This enables client and server applications to communicate and develop with connected systems in a transparent manner. We are able to observe that gRPC is being supported by a number of major technology companies, which include Google, Netflix, Square, IBM, Cisco, and Dropbox. To optimize API security, performance, and scalability, the framework uses HTTP/2, Protocol Buffers, and other current technological stacks.

For data streams between remote computers, gRPC uses HTTP/2 by default. The data can be processed using protocol buffers developed by Google. Protocol buffers are saved as plain text files closing in ‘.proto’.

How does gRPC work?

Client applications using gRPC can directly call methods of server applications on another computer as if they were local objects, making it easier to create distributed applications and services. gRPC, like many RPC systems, is based on the concept of defining a service and using parameters and return types to specify methods that can be called remotely. To efficiently handle client calls the server implements this dedicated interface and runs the gRPC server. On the client side, there is a stub (called a client in some languages) that exposes the same methods as the server.

work

Source

Communication Types of gRPC

A gRPC can have different types of services. How messages are sent and received by a service depends on the type of service defined. The gRPC communication types are:

  • Unary
    • Which is the simplest form of RPC where a request is made and a reply is provided.
  • Server Streaming
    • Here the client sends a request to be responded with a data stream rapidly.
  • Client Streaming
    • In this case, the sender transmits a stream of data as well as the server returns a single response.
  • Bi-directional Streaming
    • Where the client and server both stream data back and forth.

gRPC Development Workflow

We can now sum up the gRPC development process.

workflow

Basic arguments and return types that can be called remotely are defined, along with the services that need to be set up.

After defining the data’s structure in a file with the.proto extension, we use the protoc compiler to create data access classes in your preferred language from your proto definition. This will generate the data access classes from your application.

Implementation of the server in the chosen language & Creation of the client stub that calls the service.

Create gRPC Proto

Now that we’ve covered the basics, let’s go on to see how we can create the Service, and then how we can consume that service in the client. We’ll need a few things to get started with the service, which are as follows:

  1. Visual Studio 2022
  2. .NET Core SDK

Once you have this, we can move on to creating our first service by following the instructions below.

Step 1: Add New Project and Select the gRPC Service as a new project

CreateProject

In this Project, we are going to create our first demo. Given below is a conventional way to add the proto.

Step 2: Right Click on Protos Folder -> Add New Item -> Select Protocol Buffer File and name it “demo” like below,

Our demo service will have the following code,

democode

Code Explanation:-

  1. The version of Protobuf used in the program is specified on the first line.
  2. The Namespace under which this service will be created is specified on the second line.
  3. The next section specifies the actual service name and the Method of the Service. In our case our service is demoService and we have the SayHello Method with HelloRequest parameter and returns the HelloReply.
  4. The last part is the actual data which is a language neutral format for specifying the message sent and received by gRPC services.

Conclusion

Everything I’ve shared here so far is really only a small portion of what gRPC is, what it can do, and how it works in general. I truly and honestly hope that this article has helped you understand more about gRPC. Moreover, there is still much to discover, so don’t stop here! Continue your exploration.

Written By,

.NET Developer at Yudiz Solutions Ltd