Using gRPC in .NET 8 allows you to build high-performance, contract-first RPC (Remote Procedure Call) APIs that are ideal for internal services, microservices, or systems needing low-latency communication.

What is gRPC?

  • gRPC (Google Remote Procedure Call) is a high-performance RPC framework using HTTP/2 and Protocol Buffers (Protobuf) for serializing structured data.
  • Ideal for microservices and cross-language communication.
  • Strongly typed and contract-first (uses .proto files).

Key Features in .NET 8

  • .NET 8 supports gRPC services out of the box.
  • Integration with ASP.NET Core.
  • Support for gRPC client and server, streaming, interceptors, channel options, and more.
  • HTTP/2 support is required.

Step-by-Step: Create a gRPC Service in .NET 8

1. Create a gRPC Project

dotnet new grpc -n GrpcDemo
cd GrpcDemo

This creates:

  • Protos/greet.proto
  • A sample service implementation GreeterService.cs

2. Define the gRPC Contract: greet.proto

syntax = "proto3";
option csharp_namespace = "GrpcDemo";
package greet;
// The request message
message HelloRequest { string name = 1;
}
// The response message
message HelloReply { string message = 1;
}
// The service definition
service Greeter { rpc SayHello (HelloRequest) returns (HelloReply);
}

3. Implement the Service: GreeterService.cs

using Grpc.Core;
using GrpcDemo;
public class GreeterService : Greeter.GreeterBase
{ public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = $"Hello, {request.Name}" }); }
}

4. Register the gRPC Service in Program.cs

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
var app = builder.Build();
app.MapGrpcService<GreeterService>();
app.MapGet("/", () => "Use a gRPC client to communicate.");
app.Run();

gRPC Client Usage in .NET

1. Add gRPC Client to a Console App

dotnet new console -n GrpcClientDemo
cd GrpcClientDemo
dotnet add package Grpc.Net.Client
dotnet add package Google.Protobuf
dotnet add package Grpc.Tools

2. Add greet.proto to client project and configure it

In your .csproj:

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

3. Client Code: Program.cs

using Grpc.Net.Client;
using GrpcDemo;
using var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "Bhhomi" });
Console.WriteLine($"Greeting: {reply.Message}");

Development Notes

  • gRPC uses HTTP/2, so it works best with Kestrel, not IIS Express.
  • You can use gRPC-Web if you need to call gRPC from browsers.
  • Use dotnet dev-certs https –trust if you encounter HTTPS issues in dev.

Advanced Features

  • Streaming (Client, Server, Bi-directional)
  • Authentication (JWT, TLS)
  • Interceptors for logging, metrics, auth
  • Error handling with RpcException
  • Deadline & Cancellation

Conclusion

gRPC in .NET 8 enables fast, contract-based communication between services, ideal for microservices and internal APIs. It uses .proto files to define messages and supports features like streaming and authentication. With built-in support in .NET 8, it’s easy to set up and use for high-performance service communication.