TechBubbles Microsoft Technology BLOG

GRPC features in .NET FW 6

gRPC allows you to develop high-performance service in .NET FW 6. This post outlines the performance improvements made in gRPC in .NET 6, support to handling transient faults, client side load balancing and developing gRPC over HTTP/3. It also outlines the sample gRPC project creation using Visual Studio 2022.

What is gRPC?

gRPC is an open source remote procedure call framework allows to communicate between client and back-end services. This project run by Cloud Native Computing Foundation. Microsoft implemented the support in .NET 5 and 6. gRPC supported by most programming languages and it has cross platform support. The communication between client and service is based on contract. Contract contains the methods and messages. This contract will be used for code generation. gRPC is basically union of two modern technologies i.e. http2 is underlining protocol and protobuf is serialization technique.

Image Source: Microsoft dotnet conference

The main difference between REST and gRPC is , rather sending text based JSON messages , gRPC sends Binary ProtoBuff messages. The bigger difference is how gRPC actually returns the message, it is contract first messages and methods defined in proto files. Visual Studio provides tooling to generate service base type to server-end and strongly typed will be generated for client-end. Another main difference is gRPC works on HTTP2 and supports multiplexing meaning multiple requests can send using single connection. Advanced streaming allows to send multiple messages in response in a single connection. In other words it supports bi-directional streaming. The ProtoBuff serialization technique is small and fast.

Building a gRPC app using Visual Studio 2022

There is a gRPC template in VS 2022 for creating a new project. This template creates a brand new core app using .NET FW 6.

The new things in Project structure are .proto file and service file. greet.proto is a contact and GreeterService.cs is a implementation of proto file.

Proto file looks as follows and it basically language neutral syntax for defining a gRPC service, it contains a Greeter service that defines SayHello method. HelloRequest is a named property and HelloReply is a message property.

Implementation is in GreeterService file that looks as follows, you may be wondering where are these types coming from example: Greeter.GreeterBase, developer don’t write these types, these are generated automatically by code generation.

How this code generation works?

high-lighted Protobuf element specifies to refer .proto file and says generate server code generation.

How to call the gRPC Service?

Create a console application to call gRPC service and add a connected service reference of type gRPC

click next and provide proto file as an input to client application , it then adds the required NuGet packages

Sample Client code that calls the gRPC service looks as follows

when you run your service and client application then it shows following messages

In .NET 6 , Microsoft did some performance improvements specifically on ASCII serialization library that uses SIMD(single instruction multiple data) and process in parallel. 20% faster in serializing ProtoBuff messages compared to .NET 5.

Transient fault Handling

gRPC calls can be interrupted by Transient faults. Transient faults can be loss of network connectivity or temporarily service unavailability or timeout errors during heavy load. In order to recover from these types of faults automatically in .NET 6 , it provides an option to configure statuses to retry and how many times to retry the service. You can centralize this logic in gRPC client

Client side load balancing

gRPC supports client side load balancing by allowing client to load balance the RPC calls across multiple services without using a proxy. proxies are additional overhead on network usage, CPU and memory usage.

How it works?

In gRPC Channel , you can specify the address that you want to discover the end points from, basically it supports end-point discovery. When gRPC client starts up it calls the Discovery service and DNS service returns the list of IP addresses of servers where client uses to load balance across. When client making calls it uses load balancing policy such as round robin , pick first or custom. Client will also monitors the connection health, if any server is down then requests automatically load balanced to health end-points.

Support to HTTP/3 in preview

HTTP/3 support in .NET 6 is a preview feature. some benefits of using HTTP/3 are faster response time of the first request and improved user experience when there is connection pocket loss. It also supports transition between networks. example: if client moves from Wi-Fi to 5G. Basically you don’t kill the TCP connection and recreate it.

More information on gRPC can read from here

In the next post, I will explain how we can design and use gRPC in microservices world!

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me


Tag Cloud