gRPC in .NET: From Zero to Working Example

In today's era of microservices and cloud-native architectures, communication efficiency between services has become a key bottleneck for system performance. Are you still troubled by the inefficient serialization of REST APIs? Are you looking for a communication solution that is both cross-language and high-performance? Today, let's get to know gRPC and how the .NET ecosystem perfectly supports this modern RPC framework.

If you are already familiar with the concepts of gRPC, you can skip directly to Chapter 3; if you are already proficient with gRPC, you can skip this article entirely ^_^

1. What is gRPC? Why is it so important?

gRPC is a high-performance Remote Procedure Call framework open-sourced by Google. It is based on the HTTP/2 protocol and uses Protocol Buffers (protobuf) as the interface definition language and data serialization format.

Several core advantages of gRPC:

  • High Performance: Uses protobuf binary serialization, which is smaller and faster than JSON.

  • Multi-language Support: Supports multiple languages including C++, Java, Python, Go, and C#.

  • Bidirectional Streaming Communication: Based on HTTP/2, supports both client-side and server-side bidirectional streaming.

  • Strong Typing: Explicitly defines interfaces through .proto files, generating strongly-typed code.

  • Automatic Code Generation: Improves development efficiency and reduces the need to manually write boilerplate code.

Several communication patterns of gRPC:

  1. Unary RPC: The client sends a single request, and the server returns a single response. This is the most basic and commonly used interaction, perfectly comparable to REST API calls.

  2. Server-side Streaming RPC: The client sends a request, and the server returns a streaming response.

  3. Client-side Streaming RPC: The client sends a streaming request, and the server returns a single response.

  4. Bidirectional Streaming RPC: Both sides send streaming messages simultaneously.

2. .NET Support for gRPC: Mature and Powerful

Microsoft's support for gRPC is a full commitment. Since .NET Core 3.0, gRPC has become a first-class citizen on the .NET platform.

Official Support Status:

Component Package Name Purpose
gRPC Server Microsoft.AspNetCore.App (built-in) Create gRPC services in ASP.NET Core
gRPC Client Grpc.Net.Client Call gRPC services from .NET Core
Code Generation Tools Grpc.Tools Generate C# code from .proto files
Protobuf Support Google.Protobuf Protocol Buffers serialization support

.NET gRPC Technical Features:

  • Fully Managed Implementation: Native .NET implementation, no dependency on C++ libraries.

  • Seamless Integration with ASP.NET Core: Shares configuration, logging, dependency injection, and other infrastructure.

  • Excellent Performance: Microsoft official benchmarks show gRPC significantly outperforms traditional REST APIs.

  • Cross-platform Support: Runs perfectly on Windows, Linux, and macOS.

3. Hands-on: Creating a Complete gRPC Service and Client

Now, let's create a basic gRPC example, including both the server and the client.

3.1 Environment Setup

  • Visual Studio 2022 (requires the "ASP.NET and web development" workload, as shown below)

  • .NET 6.0 SDK or higher

3.2 Create the gRPC Server

Step 1: Create the project

  1. Launch Visual Studio and select "Create a new project".

  2. Search for "gRPC" and select the "ASP.NET Core gRPC Service" template.

  1. Name the project GrpcGreeter.

  2. Select the .NET version (recommend .NET 8.0 or higher).

Step 2: Examine the project structure
After creation, you will see the following core files:

  • Protos/greet.proto: The interface definition file for the gRPC service. It contains a request/response data structure and an interface function SayHello.

  • Services/GreeterService.cs: Implements the business logic of the gRPC service.

Step 3: Run the server
Press F5 to run the service. The console will display the address the service is listening on, e.g., https://localhost:7208.

3.3 Create the gRPC Client

Step 1: Create a Console App

  1. Open another instance of Visual Studio.

  2. Create a "Console App" project and name it GrpcGreeterClient.

Add the following NuGet packages to the client console project; their purposes are described at the beginning of the article.

Step 2: Add the proto file

  1. Create a Protos folder in the client project and copy the greet.proto file from the server into this folder.

  2. Modify the namespace in greet.proto to distinguish it (optional, not modifying it also works fine).

Edit the .csproj file to include the proto file in the project's build process:

xml
<ItemGroup>
    <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>

Step 3: Write the client code and run it
Add our request code to the console project, and you will receive a response from the server. SayHelloAsync is an asynchronous function auto-generated from the Proto file. Here we have completed a complete RPC interaction using the simplest unary RPC mode of gRPC. Note that no valid HTTPS certificate was added for the server, so using HTTPS directly would cause certificate errors. Therefore, HTTP is used temporarily.

4. Summary: Typical Use Cases for gRPC

✅ Scenarios suitable for gRPC:

  • Microservices Communication: High-performance, low-latency internal service calls.

  • Real-time Services: Scenarios requiring streaming data transmission (e.g., chat, location sync).

  • Multi-language Environments: Services written in different languages need efficient communication.

  • Mobile Clients: Binary transmission saves traffic and battery life.

❌ Although gRPC is powerful, there are scenarios where it is less suitable compared to REST APIs:

  • Direct Browser Calls: Requires a grpc-web proxy, less convenient than REST.

  • Public-facing APIs: Requires broad client support.

  • Simple Query Scenarios: When the overhead of REST is acceptable, there's no need for additional complexity.

Through the introduction in this article, you can see .NET's comprehensive support for gRPC. From creating the server to writing the client, the entire process is smooth and natural. gRPC's features, such as high performance, strong typing, and multi-language support, make it a powerful communication tool for modern microservice architectures.

With the continuous development of the .NET platform, the application of gRPC in .NET will become even more widespread. The official team is also constantly optimizing the performance and experience of gRPC, positioning it as the preferred solution for future service-to-service communication.