In this post, I'll share some details about the gRPC framework, and then there will be a short comparison between gRPC and REST-based HTTP APIs; in the end, you will find a simple gRPC service example on .NET Core. You can see the full code of this service in this Github repository.

What is gRPC?

gRPC is a modern open-source, high-performance Remote Procedure Call framework that can run in any environment. gRPC helps us to implement high-quality services in every language; C/C++, C#, Java, Go, Python and many programming languages officially support gRPC. Many of the companies like Square, Netflix, CoreOS, Cisco, Carbon3D, Cockroach labs are using GRPC in different scenarios. gRPC is a framework developed by Google, and it's open-source now. It relies on Protocol Buffers that helps us to communicate with other services independently from the programming language.

What Are The Main Benefits Of Using gRPC?

  • Lightweight, high-performance and language agnostic framework.
  • Great tooling for testing, inspection, and modification.
  • Supports client, server, and bi-directional streaming calls with HTTP/2 based transport.
  • Reduces network usage with Protobuf binary serialization.
gRPC clients and servers can run and talk to each other in a variety of environments (source: grpc.io).

REST-based HTTP APIs and gRPC

While HTTP APIs use HTTP 1.1, gRPC uses HTTP/2 that makes gRPC faster using less memory. HTTP/2 supports full-duplex, or bidirectional communication, where both client and server and can communicate at the same time, which is not possible in HTTP APIs. Combining gRPC and HTTP/2, performance dramatically increases. However it's easy to understand and use HTTP APIs over gRPC, and there aren't too many restrictions about data contracts or browsers. gRPC isn't fully supported in the browser; it's not possible to directly call a gRPC service from a browser today. gRPC heavily uses HTTP/2 features, and no browser provides the level of control required over web requests to support a gRPC client. HTTP APIs requests are sent as text (mostly JSON) and can be read and created by humans while gRPC messages are encoded with Protobuf by default which is in binary format and not human readable. You can use both HTTP APIs and gRPC in different use-cases. It can be a good idea to replace HTTP APIs with gRPC to provide low latency and high throughput communication in some services where efficiency is critical.

gRPC support in .NET Core

The Microsoft .NET Core framework 3+ includes tooling and native support for gRPC.

Creating a gRPC Service

We'll create a simple gRPC service that returns the US shoe size for an EU size. You should have .NET Core 3+ version installed in your computer to be able to create this service.

Create gRPC Service:
> dotnet new grpc -o ShoeSizeConversionService

Go to the service folder:
> cd ShoeSizeConversionService

Create the contract with proto3:

Then we need to update the CreditRatingService.csproj file to complete gRPC contract configuration.

Now that our contract is ready, so we can implement the service:

You can run and test the service by typing the following command in your terminal window:
> dotnet run

If you have some issues on different environments you may need to make some configurations changes.

Creating a gRPC Client

Now we'll create a simple Console application to consume our service.

Create gRPC client:
> dotnet new console -o ShoeSizeConversionClient

Go to the client folder:
> cd ShoeSizeConversionClient

Add gRPC dependencies:
> dotnet add CreditRatingClient.csproj package Grpc.Net.Client
> dotnet add CreditRatingClient.csproj package Google.Protobuf
> dotnet add CreditRatingClient.csproj package Grpc.Tools

You need to create a Protos folder and copy the shoe-size-conversion-service.proto file under this folder. You need to update the CreditRatingService.csproj file to complete gRPC contract configuration as we did before for the service.

Please don't forget to build the project after this step:
> dotnet build

We are ready to consume the service now.

You can run and test the client by typing the following command in your terminal window:
> dotnet run

And the result will be as shown below:
> 11.5 Women US → 43 EU

Happy coding!