gRPC for C# (.NET): Libraries and Usage Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

gRPC for C# (.NET): Libraries and Examples



gRPC is a high-performance remote procedure call (RPC) framework by Google, based on HTTP/2 and Protocol Buffers (protobuf). In the .NET ecosystem, gRPC has become the de facto standard for microservice communication, providing strong typing, bidirectional streaming, and low latency. In this article, we will review the key libraries for working with gRPC in C# and provide practical examples of server and client setup.



Main gRPC Libraries for .NET



The following NuGet packages are used for working with gRPC in .NET:



  • Grpc.AspNetCore — integration of gRPC server into ASP.NET Core (recommended for hosting).
  • Grpc.Net.Client — client library for .NET (including .NET Core, .NET 5+).
  • Grpc.Tools — code generator from .proto files (added as a Protobuf item in .csproj).
  • Google.Protobuf — working with protobuf messages.


All these packages are supported by Microsoft and are actively developed. Installation is done via NuGet Package Manager or dotnet CLI:



dotnet add package Grpc.AspNetCoredotnet add package Grpc.Net.Clientdotnet add package Google.Protobufdotnet add package Grpc.Tools


Creating a gRPC Server in C#



Let's go through the step-by-step creation of a simple service for working with orders.



1. Defining the proto file

Create a file order.proto in the server project (e.g., GrpcServer):



syntax = "proto3";

service OrderService { rpc GetOrder (OrderRequest) returns (OrderResponse); rpc SubscribeOrders (OrderRequest) returns (stream OrderResponse);}

message OrderRequest { int32 order_id = 1;}

message OrderResponse { int32 order_id = 1; string status = 2; double total = 3;}


2. Configuring .csproj for code generation

Add to the .csproj file:



<ItemGroup>    <Protobuf Include="order.proto" GrpcServices="Server" /></ItemGroup>


3. Implementing the service

Create a class inheriting from the generated base class:



public class OrderServiceImpl : OrderService.OrderServiceBase{    public override Task<OrderResponse> GetOrder(OrderRequest request, ServerCallContext context)    {        return Task.FromResult(new OrderResponse        {            OrderId = request.OrderId,            Status = "Shipped",            Total = 99.99        });    }

public override async Task SubscribeOrders(OrderRequest request, IServerStreamWriter<OrderResponse> responseStream, ServerCallContext context) { for (int i = 0; i < 5; i++) { await responseStream.WriteAsync(new OrderResponse { OrderId = request.OrderId, Status = $"Update #{i}", Total = 100 + i * 10 }); await Task.Delay(1000); } }}


4. Registration in Program.cs



var builder = WebApplication.CreateBuilder(args);builder.Services.AddGrpc();

var app = builder.Build();app.MapGrpcService<OrderServiceImpl>();app.MapGet("/", () => "gRPC server is running");app.Run();


Creating a gRPC Client in C#



Now let's create a console client application (GrpcClient).



1. Preparation

Copy order.proto to the client project and specify in .csproj:



<Protobuf Include="order.proto" GrpcServices="Client" />


2. Client code



using Grpc.Net.Client;using GrpcClient;

var channel = GrpcChannel.ForAddress("https://localhost:5001");\

Recommendations