How grpc/grpc-dotnet works
Project Type and Purpose
• This is an open‐source framework & toolset (“gRPC for .NET”) that provides:
• A core API for defining gRPC services (Grpc.Core.Api)
• Shared infrastructure & helpers (Shared)
• .NET client libraries (Grpc.Net.Client, Grpc.Net.ClientFactory, Grpc.Net.Client.Web)
• ASP.NET Core server integration (Grpc.AspNetCore.Server, plus extensions for HealthChecks, Reflection, ClientFactory, Web‐Middleware)
• Ancillary packages for authentication, health checking, reflection, status proto, etc.
• A CLI tool (dotnet-grpc) to scaffold and manage .proto files
• Examples, performance benchmarks, and comprehensive tests
File‐Structure Overview
• Top‐level solution (Grpc.DotNet.slnx)
• /src: All the class‐library packages and CLI tool that form the core framework
• /examples: Dozens of small end‐to‐end sample apps demonstrating client/server scenarios
• /perf: Benchmark projects reflecting real‐world performance tests
• /test: Functional/unit tests for each package
• /doc: Design notes, release process, versioning, planning
• Build scripts (build/, Directory.Build.props/targets) and CI definitions (.github/workflows/)
Main Components & Their Relationships
A. Core Layers
1. Grpc.Core.Api
– Defines the RPC abstractions (Channel, CallInvoker, Metadata, Marshaller, Context)
2. Shared
– Common helpers (HTTP/2 error codes, pipe extensions, timeout helpers)
B. Client Stack
1. Grpc.Net.Client
– High-level gRPC client built on HttpClient/HTTP/2
2. Grpc.Net.ClientFactory
– Integrates gRPC clients with IHttpClientFactory & DI
3. Grpc.Net.Client.Web
– Enables gRPC-Web scenarios (base64 encoding over HTTP/1.1)
C. Server Stack (ASP.NET Core Integration)
1. Grpc.AspNetCore.Server
– Core middleware, endpoint mapping, call handlers, interceptor support
2. Grpc.AspNetCore.HealthChecks
– Exposes gRPC health‐check endpoints via ASP.NET health‐check pipeline
3. Grpc.AspNetCore.Server.ClientFactory
– Allows server‐side code to create gRPC clients with context propagation
4. Grpc.AspNetCore.Server.Reflection
– Exposes reflection service so clients can discover service methods at runtime
5. Grpc.AspNetCore.Web
– Middleware enabling browser clients (gRPC-Web)
D. Ecosystem Packages
– Grpc.Auth (Google auth interceptors)
– Grpc.HealthCheck (server‐side health service)
– Grpc.Reflection (reflection service implementation)
– Grpc.StatusProto (maps RPC status to Google.Rpc.Status)
E. Tooling & Samples
– dotnet-grpc CLI: scaffolding commands, project file modification
– /examples: individual “microservices” illustrating patterns (Unary, Streaming, Auth, TLS, Load‐balancing)
– /perf: performance harness and microbenchmarks
How
– Core API & Shared at the center/bottom as a foundational layer.
– Above it, two parallel stacks: Client libraries on the left, Server libraries on the right.
– All client/server packages → depend on Core/API & Shared.
– ClientFactory → depends on Grpc.Net.Client & Microsoft DI.
– Server extensions (HealthChecks, Reflection) → depend on Grpc.AspNetCore.Server.
– dotnet-grpc → depends on Core/API & filesystem/project files.
c. Run‐Time Data Flow (in a call flow inset):
• Client App → GrpcChannel (Grpc.Net.Client) → HTTP/2 transport → Kestrel (ASP.NET Core) → gRPC middleware (Grpc.AspNetCore.Server) → Service Handler → returns through reverse path.
• gRPC-Web: add Grpc.AspNetCore.Web between HTTP/1.1 requests and gRPC middleware.
d. Tailor to an Open‐Source Library Project:
– Emphasize core libraries and extension‐points (DI, middleware, interceptors).
– Show clearly the “extensible plugin layers”: interceptors, health checks, reflection, code‐gen CLI.
– Group Examples/Perf/Test as separate “consumer” projects that sit on top of the framework.
Important Architectural Patterns & Principles
– Layered architecture (Core API → Implementation → Extensions)
– Dependency Injection & Middleware pipeline (leveraging ASP.NET Core)
– Separation of transport (HTTP/2 & HTTP/1.1 via gRPC-Web) from application logic
– Interceptor model for cross‐cutting concerns (logging, auth, retry)
– Code‐generation versus manual contract definitions (dotnet-grpc CLI vs. protobuf‐first)