Getting Started with gRPC and .NET 7

Sachin Patil
3 min readNov 21, 2022

--

Get in a topic:

gRPC (Remote Procedure Call) protocol, gRPC comes into the API. It is a free and open-source framework developed by Google that utilizes HTTP/2 for API communication, hiding the HTTP implementation from the API designer

gRPC has many characteristics that make it a foundation block for the next generation of web applications, from microservices to Web/mobile API communication:

  • gRPC JSON transcoding: Transcoding allows browser apps to call gRPC services as if they were RESTful APIs with JSON. The browser app doesn’t need to generate a gRPC client or know anything about gRPC.
  • grpc-gateway: gRPC-Gateway is a plugin of protoc. It reads a gRPC service definition and generates a reverse-proxy server which translates a RESTful JSON API into gRPC. This server is generated according to custom options in your gRPC definition.
Source — https://grpc-ecosystem.github.io/grpc-gateway/
  • Interoperability: regardless of the current HTTP version, the protocol must be able to adapt and change no matter how the infrastructure changes (upgrading from HTTP 2 to HTTP 3 for example).
  • Layered architecture: key facets of the stack must be able to evolve and be upgraded independently without the need to disrupt any application that uses it.
  • Payload Agnostic: different services can require different message types and encodings such as Protobuf (Protocol buffer), Json, XML, and many others. gRPC allows for all that and also for payload compression by utilizing pluggable compression mechanisms.
  • Streaming: gRPC allows the relay of large data sets from server to client and vice versa.
  • Pluggable: gRPC is built to plug different functionalities and services that are required to meet our demands, such as health checks, fail-overs and load balancing. The framework implementation provides extension points that allow for plugging in these functionalities.

gRPC Component

  • Proto File: gRPC uses a contract-first approach to API development. Protocol buffers (protobuf) are used as the Interface Definition Language (IDL) by default. The .proto file contains The definition of the gRPC service and The messages sent between clients and servers.
  • The gRPC code will be generated for us and will need to provide an implementation
  • One .proto file is supported by 12 different languages for both server and client

gRPC core architecture

Download Code here

Microservices case study

The challenge in designing and developing a capability that need to be consumed internally by other services and exposed to 3rd party systems or users

Let us consider a sample scenario, of an order management system consisting of an order manager and product inventory microservice.

  • The Product Inventory service holds all the product details & its relationships, including the various categories. REST API is required to expose the product details & their relationships to external systems and user interfaces.
  • The Order Manager service interfaces with another digital channel which acts as the front-end system for customer orders. This internally invokes the Product Inventory service to validate the product inventory details.

In the current scheme of things, there are multiple approaches possible where we could address such a requirement, a few such options are detailed below: –

Following an approach where any inter-service communication leverage gRPC and for any client-facing services leverage REST.

Source — Cloud Native

Nutshell

the benefits of gRPC are:

  • modern, fast
  • open source
  • utilize HTTP/2
  • Language neutral
  • Easy to add authentication and logging.

Impact for development

  • gRPC need much more setup time and coordination between the client and the server
  • REST — get started directly by consuming endpoints almost without any setup.
  • Expose data as REST (JSON based) internally and externally: -This most popular approach does not cater to all requirements. This is not ideal for data-intensive inter-service communications due to JSON payloads and HTTP protocol limitations.

It is important to think through and consider these various integration patterns while we consider designing our next microservices-based solutions.

Download Code here

--

--

Sachin Patil
Sachin Patil

Written by Sachin Patil

Software Development | Microsoft Azure Certified | Kubernetes Certified | Google Cloud Architect Certified| Microservice |Agile Enthusiast

No responses yet