gRPC for .NET
Trigger On
- building backend-to-backend RPC services or clients
- adding protobuf contracts, streaming calls, or interceptors
- deciding between gRPC, HTTP APIs, and SignalR
- optimizing gRPC performance, deadlines, cancellation, or connection reuse
- integrating service-to-service communication in microservices
Do Not Use For
- public browser-first APIs unless gRPC-Web limitations are explicitly acceptable
- SignalR hub design, realtime UI fan-out, or websocket-style client collaboration
- generic ASP.NET Core minimal APIs or REST controllers with no protobuf/RPC requirement
- non-.NET gRPC work unless the user asks for cross-stack contract guidance
Load References
- references/patterns.md for proto design, streaming implementations, interceptors, health checks, load balancing, and client factory setup.
- references/anti-patterns.md for common channel, deadline, streaming, message-size, and exception-handling mistakes.
Workflow
- Validate the architecture fit before touching code.
- prefer gRPC for backend RPC, strong contracts, low-latency calls, or streaming
- prefer REST or minimal APIs for broad browser compatibility and loosely coupled public APIs
- prefer SignalR for browser/client realtime fan-out and UI collaboration
- Treat
.proto files as the source of truth.
- keep package names,
csharp_namespace, service names, and versioning deliberate
- reserve removed field numbers and avoid reusing tags
- use wrapper types or explicit messages when optionality matters
- Choose the RPC shape from the interaction model.
- unary for request/response
- server streaming for large or progressive result sets
- client streaming for uploads or batches
- bidirectional streaming for coordinated two-way flows
- Wire server and client behavior together.
- register services with
AddGrpc
- use
AddGrpcClient or long-lived GrpcChannel reuse
- set deadlines and propagate cancellation
- convert domain failures to appropriate
RpcException status codes
- Add observability and resilience where the boundary justifies it.
- logging or exception interceptors
- OpenTelemetry traces and status-code metrics
- retry policy only for safe idempotent calls
- Validate with the repo's normal build and tests, plus a focused smoke call when runnable.
Current Upstream Notes
dotnet/aspnetcore v10.0.11 is servicing and does not change the gRPC programming model. Keep guidance focused on proto compatibility, streaming shape, deadlines, cancellation, channel reuse, and smoke calls.
- The August 2026 ASP.NET Core overview still treats gRPC as a contract-first RPC option. After package servicing updates, regenerate protobuf outputs only when inputs or generator packages actually changed; do not churn generated files as a proxy for validation.
flowchart LR
A["RPC requirement"] --> B["proto contract"]
B --> C["server implementation"]
B --> D["client factory or channel"]
C --> E["deadlines / cancellation / status codes"]
D --> E
E --> F["build, tests, smoke call"]
Examples
Use client factory for normal app integration:
builder.Services.AddGrpcClient<Greeter.GreeterClient>(options =>
{
options.Address = new Uri("https://localhost:5001");
});
Always set a deadline and pass cancellation:
var response = await client.SayHelloAsync(
new HelloRequest { Name = name },
deadline: DateTime.UtcNow.AddSeconds(5),
cancellationToken: cancellationToken);
For streaming, check cancellation inside the read/write loop and keep message sizes bounded. Load references/patterns.md before writing detailed streaming code.
Anti-Patterns
- creating a new
GrpcChannel per call
- omitting deadlines and relying only on client-side cancellation
- ignoring
ServerCallContext.CancellationToken in streaming handlers
- sending large single messages instead of chunking or streaming
- using gRPC as the default public browser API
- swallowing exceptions inside interceptors
- retrying non-idempotent calls without explicit policy
Deliver
- stable protobuf contracts and generated-code ownership
- service and client code that match the RPC shape
- explicit deadline, cancellation, retry, and status-code behavior
- tests or smoke checks for serialization and call behavior
- documentation of browser, transport, or deployment constraints when relevant
Validate
dotnet build succeeds after contract or generated-code changes
- tests or smoke checks exercise at least one server/client call
- streaming methods respect cancellation and bounded message sizes
- channels are reused through client factory or a long-lived channel
- status-code handling is intentional and observable
- browser constraints are documented if gRPC-Web is involved
1---2name: grpc3description: Build or review gRPC services and clients in .NET. USE FOR: ASP.NET Core gRPC, protobuf contracts, unary or streaming RPC, gRPC client factory, interceptors, deadlines, cancellation, channel reuse, backend service integration. DO NOT USE FOR: broad browser-facing APIs without gRPC-Web tradeoff review, SignalR realtime hubs, plain REST APIs. INVOKES: dotnet build/test and focused service or client smoke checks when code changes.4---56# gRPC for .NET78## Trigger On910- building backend-to-backend RPC services or clients11- adding protobuf contracts, streaming calls, or interceptors12- deciding between gRPC, HTTP APIs, and SignalR13- optimizing gRPC performance, deadlines, cancellation, or connection reuse14- integrating service-to-service communication in microservices1516## Do Not Use For1718- public browser-first APIs unless gRPC-Web limitations are explicitly acceptable19- SignalR hub design, realtime UI fan-out, or websocket-style client collaboration20- generic ASP.NET Core minimal APIs or REST controllers with no protobuf/RPC requirement21- non-.NET gRPC work unless the user asks for cross-stack contract guidance2223## Load References2425- [references/patterns.md](references/patterns.md) for proto design, streaming implementations, interceptors, health checks, load balancing, and client factory setup.26- [references/anti-patterns.md](references/anti-patterns.md) for common channel, deadline, streaming, message-size, and exception-handling mistakes.2728## Workflow29301. Validate the architecture fit before touching code.31 - prefer gRPC for backend RPC, strong contracts, low-latency calls, or streaming32 - prefer REST or minimal APIs for broad browser compatibility and loosely coupled public APIs33 - prefer SignalR for browser/client realtime fan-out and UI collaboration342. Treat `.proto` files as the source of truth.35 - keep package names, `csharp_namespace`, service names, and versioning deliberate36 - reserve removed field numbers and avoid reusing tags37 - use wrapper types or explicit messages when optionality matters383. Choose the RPC shape from the interaction model.39 - unary for request/response40 - server streaming for large or progressive result sets41 - client streaming for uploads or batches42 - bidirectional streaming for coordinated two-way flows434. Wire server and client behavior together.44 - register services with `AddGrpc`45 - use `AddGrpcClient` or long-lived `GrpcChannel` reuse46 - set deadlines and propagate cancellation47 - convert domain failures to appropriate `RpcException` status codes485. Add observability and resilience where the boundary justifies it.49 - logging or exception interceptors50 - OpenTelemetry traces and status-code metrics51 - retry policy only for safe idempotent calls526. Validate with the repo's normal build and tests, plus a focused smoke call when runnable.5354## Current Upstream Notes5556- `dotnet/aspnetcore` `v10.0.11` is servicing and does not change the gRPC programming model. Keep guidance focused on proto compatibility, streaming shape, deadlines, cancellation, channel reuse, and smoke calls.57- The August 2026 ASP.NET Core overview still treats gRPC as a contract-first RPC option. After package servicing updates, regenerate protobuf outputs only when inputs or generator packages actually changed; do not churn generated files as a proxy for validation.5859```mermaid60flowchart LR61 A["RPC requirement"] --> B["proto contract"]62 B --> C["server implementation"]63 B --> D["client factory or channel"]64 C --> E["deadlines / cancellation / status codes"]65 D --> E66 E --> F["build, tests, smoke call"]67```6869## Examples7071Use client factory for normal app integration:7273```csharp74builder.Services.AddGrpcClient<Greeter.GreeterClient>(options =>75{76 options.Address = new Uri("https://localhost:5001");77});78```7980Always set a deadline and pass cancellation:8182```csharp83var response = await client.SayHelloAsync(84 new HelloRequest { Name = name },85 deadline: DateTime.UtcNow.AddSeconds(5),86 cancellationToken: cancellationToken);87```8889For streaming, check cancellation inside the read/write loop and keep message sizes bounded. Load [references/patterns.md](references/patterns.md) before writing detailed streaming code.9091## Anti-Patterns9293- creating a new `GrpcChannel` per call94- omitting deadlines and relying only on client-side cancellation95- ignoring `ServerCallContext.CancellationToken` in streaming handlers96- sending large single messages instead of chunking or streaming97- using gRPC as the default public browser API98- swallowing exceptions inside interceptors99- retrying non-idempotent calls without explicit policy100101## Deliver102103- stable protobuf contracts and generated-code ownership104- service and client code that match the RPC shape105- explicit deadline, cancellation, retry, and status-code behavior106- tests or smoke checks for serialization and call behavior107- documentation of browser, transport, or deployment constraints when relevant108109## Validate110111- `dotnet build` succeeds after contract or generated-code changes112- tests or smoke checks exercise at least one server/client call113- streaming methods respect cancellation and bounded message sizes114- channels are reused through client factory or a long-lived channel115- status-code handling is intentional and observable116- browser constraints are documented if gRPC-Web is involved