Protobuf Extraction from C# Contracts
This document explains how to extract .proto files from C# gRPC service definitions using the ProtoGenerator tool.
Overview
Section titled “Overview”Chronicle uses a code-first approach for gRPC service definitions in C#. To support clients in other languages (TypeScript, Python, Go, etc.), we extract the service definitions to standard Protocol Buffer (.proto) files using the ProtoGenerator tool.
ProtoGenerator Tool
Section titled “ProtoGenerator Tool”Location: Source/Tools/ProtoGenerator
A .NET console application that generates .proto files from the Chronicle C# gRPC contracts.
How It Works
Section titled “How It Works”- Loads the
Cratis.Chronicle.Contracts.dllassembly - Finds all interfaces decorated with
[Service]attribute - Uses
protobuf-net.Grpc.Reflectionto generate proto files - Groups services by namespace to handle multiple packages
- Outputs separate
.protofiles for each service namespace
dotnet run --project Source/Tools/ProtoGenerator/ProtoGenerator.csproj -- \ <path-to-contracts-dll> \ <output-directory>Example
Section titled “Example”dotnet run --project Source/Tools/ProtoGenerator/ProtoGenerator.csproj -- \ Source/Kernel/Contracts/bin/Release/net10.0/Cratis.Chronicle.Contracts.dll \ Source/Kernel/ProtobufGenerating Proto Files Locally
Section titled “Generating Proto Files Locally”# Build the Contracts assemblycd Source/Kernel/Contractsdotnet build -c Release
# Generate proto filescd ../../..dotnet run --project Source/Tools/ProtoGenerator/ProtoGenerator.csproj -- \ Source/Kernel/Contracts/bin/Release/net10.0/Cratis.Chronicle.Contracts.dll \ Source/Kernel/ProtobufGenerated Proto Files
Section titled “Generated Proto Files”Output location: Source/Kernel/Protobuf/*.proto
The tool generates the following proto files:
clients.proto- Client connection servicescratis_chronicle_contracts.proto- Event stores and namespacesevents.proto- Event type servicesevents_constraints.proto- Event constraint serviceseventsequences.proto- Event sequence serviceshost.proto- Host/server informationidentities.proto- Identity servicesjobs.proto- Job managementobservation.proto- Observer servicesobservation_reactors.proto- Reactor servicesobservation_reducers.proto- Reducer servicesprojections.proto- Projection servicesreadmodels.proto- Read model servicesrecommendations.proto- Recommendation servicesseeding.proto- Event seeding services
Using Proto Files for Other Clients
Section titled “Using Proto Files for Other Clients”Once the proto files are generated, they can be used to generate client code in any language that supports gRPC:
Python Example
Section titled “Python Example”python -m grpc_tools.protoc \ -I Source/Kernel/Protobuf \ --python_out=. \ --grpc_python_out=. \ Source/Kernel/Protobuf/*.protoGo Example
Section titled “Go Example”protoc \ -I Source/Kernel/Protobuf \ --go_out=. \ --go-grpc_out=. \ Source/Kernel/Protobuf/*.protoTypeScript Example
Section titled “TypeScript Example”See TypeScript gRPC Package for details on the TypeScript client generation.
Maintenance
Section titled “Maintenance”When adding new gRPC services to the C# Contracts:
- Decorate service interfaces with
[Service]attribute - Decorate service methods with
[Operation]attribute - Run the ProtoGenerator to regenerate proto files
- Regenerate client code for all supported languages
- Commit the updated proto files to the repository
Dependencies
Section titled “Dependencies”The ProtoGenerator tool uses:
protobuf-net.Grpc.Reflection- For extracting proto definitions from C# attributes.NET 10- Runtime for the tool
Proto File Format
Section titled “Proto File Format”The generated proto files follow the Protocol Buffers version 3 syntax and include:
- Service definitions with RPC methods
- Message types for requests and responses
- Import statements for dependencies (including
protobuf-net/bcl.protofor .NET-specific types like Guid, DateTime, etc.)