Skip to content

Commit 2856261

Browse files
committed
docs: add guide for automatic C# class generation from Avro and Protobuf schemas
1 parent c5d3c6d commit 2856261

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

docs/utilities/kafka.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,4 +898,74 @@ Testing Kafka consumer functions is straightforward with Xunit. You can create s
898898
}
899899
}
900900
901-
```
901+
```
902+
903+
## Code Generation for Serialization
904+
905+
This guide explains how to automatically generate C# classes from Avro and Protobuf schema files in your Lambda projects.
906+
907+
### Avro Class Generation
908+
909+
#### Prerequisites
910+
911+
Install the Apache Avro Tools globally:
912+
913+
```bash
914+
dotnet tool install --global Apache.Avro.Tools
915+
```
916+
917+
#### MSBuild Integration
918+
919+
Add the following target to your `.csproj` file to automatically generate Avro classes during compilation:
920+
921+
```xml
922+
<Target Name="GenerateAvroClasses" BeforeTargets="CoreCompile">
923+
<Exec Command="avrogen -s $(ProjectDir)CustomerProfile.avsc $(ProjectDir)Generated"/>
924+
</Target>
925+
```
926+
927+
This target will:
928+
- Run before compilation
929+
- Generate C# classes from `CustomerProfile.avsc` schema file
930+
- Output generated classes to the `Generated` folder
931+
932+
### Protobuf Class Generation
933+
934+
#### Package Reference
935+
936+
Add the Grpc.Tools package to your `.csproj` file:
937+
938+
```xml
939+
<PackageReference Include="Grpc.Tools" Version="2.72.0">
940+
<PrivateAssets>all</PrivateAssets>
941+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
942+
</PackageReference>
943+
```
944+
945+
#### Schema Files Configuration
946+
947+
Add your `.proto` files to the project with the following configuration:
948+
949+
```xml
950+
<ItemGroup>
951+
<Protobuf Include="CustomerProfile.proto">
952+
<GrpcServices>Client</GrpcServices>
953+
<Access>Public</Access>
954+
<ProtoCompile>True</ProtoCompile>
955+
<CompileOutputs>True</CompileOutputs>
956+
<OutputDir>obj\Debug/net8.0/</OutputDir>
957+
<Generator>MSBuild:Compile</Generator>
958+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
959+
</Protobuf>
960+
</ItemGroup>
961+
```
962+
963+
This configuration will:
964+
- Generate client-side gRPC services
965+
- Make generated classes public
966+
- Automatically compile and include generated files
967+
- Copy proto files to output directory
968+
969+
### Generated Code Usage
970+
971+
Both Avro and Protobuf generators create strongly-typed C# classes that can be used with the PowerTools serialization utilities for efficient Lambda function processing.

0 commit comments

Comments
 (0)