# grpc/grpc-dotnet architecture

> Architecture diagram of the GitHub repository grpc/grpc-dotnet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/grpc/grpc-dotnet
- Repository: https://github.com/grpc/grpc-dotnet

## Overview

##### 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)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Consumers
    subgraph "Consumers"  
        style Consumers stroke-dasharray: 5 5,stroke:#999
        Examples["Examples"]  
        Tests["Tests"]  
        Perf["Perf"]  
    end

    %% Client Stack
    subgraph "Client Stack"  
        direction TB
        Client["Grpc.Net.Client"]:::client
        ClientFactory["Grpc.Net.ClientFactory"]:::client
        ClientWeb["Grpc.Net.Client.Web"]:::client
    end

    %% Server Stack
    subgraph "Server Stack"  
        direction TB
        Server["Grpc.AspNetCore.Server"]:::server
        HealthChecks["Grpc.AspNetCore.HealthChecks"]:::server
        ServerClientFactory["Grpc.AspNetCore.Server.ClientFactory"]:::server
        ReflectionServer["Grpc.AspNetCore.Server.Reflection"]:::server
        WebServer["Grpc.AspNetCore.Web"]:::server
    end

    %% Ecosystem Packages
    subgraph "Ecosystem Packages"  
        direction TB
        Auth["Grpc.Auth"]:::server
        HealthService["Grpc.HealthCheck"]:::server
        ReflectionPkg["Grpc.Reflection"]:::server
        StatusProto["Grpc.StatusProto"]:::server
    end

    %% Core & Shared
    subgraph "Core Layer"  
        direction TB
        CoreApi["Grpc.Core.Api"]:::core
        Shared["Shared"]:::core
        CLI["dotnet-grpc CLI"]:::tool
    end

    %% Dependencies
    Client -->|"depends on"| CoreApi
    Client -->|"depends on"| Shared
    ClientFactory -->|"depends on"| Client
    ClientFactory -->|"depends on"| MicrosoftDI["IHttpClientFactory"]:::external
    ClientWeb -->|"depends on"| Client

    Server -->|"depends on"| CoreApi
    Server -->|"depends on"| Shared
    HealthChecks -->|"depends on"| Server
    ServerClientFactory -->|"depends on"| Server
    ReflectionServer -->|"depends on"| Server
    WebServer -->|"depends on"| Server

    Auth -->|"depends on"| Server
    HealthService -->|"depends on"| Server
    ReflectionPkg -->|"depends on"| Server
    StatusProto -->|"depends on"| Server

    CLI -->|"uses for code-gen"| CoreApi
    CLI -->|"uses for code-gen"| Shared

    %% Consumers usage
    Examples -->|"build-time & run-time"| Client
    Examples -->|"build-time & run-time"| Server
    Perf -->|"benchmarks"| Client
    Perf -->|"benchmarks"| Server
    Tests -->|"tests"| CoreApi
    Tests -->|"tests"| Client
    Tests -->|"tests"| Server

    %% Run-time Flow Inset
    subgraph "Run-Time Call Flow"
        direction TB
        App["Client App"]:::external
        Channel["GrpcChannel"]:::client
        HTTP2["HTTP/2 Transport"]:::transport
        Kestrel["Kestrel (ASP.NET Core)"]:::external
        Middleware["gRPC Middleware"]:::server
        Handler["Service Handler"]:::server

        App -->|"calls"| Channel
        Channel -->|"HTTP/2"| HTTP2
        HTTP2 --> Kestrel
        Kestrel --> Middleware
        Middleware --> Handler
        Handler --> Middleware
        Middleware --> Kestrel
        Kestrel --> HTTP2
        HTTP2 --> Channel
        Channel --> App
    end

    %% Click Events
    click CoreApi "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.Core.Api/"
    click Shared "https://github.com/grpc/grpc-dotnet/tree/master/src/Shared/"
    click Client "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.Net.Client/"
    click ClientFactory "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.Net.ClientFactory/"
    click ClientWeb "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.Net.Client.Web/"
    click Server "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.AspNetCore.Server/"
    click HealthChecks "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.AspNetCore.HealthChecks/"
    click ServerClientFactory "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.AspNetCore.Server.ClientFactory/"
    click ReflectionServer "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.AspNetCore.Server.Reflection/"
    click WebServer "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.AspNetCore.Web/"
    click Auth "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.Auth/"
    click HealthService "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.HealthCheck/"
    click ReflectionPkg "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.Reflection/"
    click StatusProto "https://github.com/grpc/grpc-dotnet/tree/master/src/Grpc.StatusProto/"
    click CLI "https://github.com/grpc/grpc-dotnet/tree/master/src/dotnet-grpc/"

    %% Styles
    classDef core fill:#dae8fc,stroke:#6c8ebf,color:#1f365d
    classDef client fill:#d5e8d4,stroke:#82b366,color:#264d23
    classDef server fill:#f8cecc,stroke:#b85450,color:#5c1412
    classDef tool fill:#e1e1e1,stroke:#888888,color:#333333
    classDef external fill:#f5f5f5,stroke:#aaaaaa,color:#555555
    classDef transport fill:#fff2cc,stroke:#d6b656,color:#665c03
```
