DEV Community

Cain Voong
Cain Voong

Posted on

Add AVDL Support to a .NET Project

By now, we have a good grasp on:

  • Taking Avro IDL files and generating C# classes
  • Automating the above so we can do it on lots of files with ease

In this final step, I will create a C# project and configure it in a way so developers only need to add/modify avdl files and build.

Create the Project

We'll start from scratch, by creating the project and then adding an Avro folder:

dotnet new classlib --name MyProject
cd MyProject
mkdir Avro
Enter fullscreen mode Exit fullscreen mode

Add the package Confluent.SchemaRegistry.Serdes.Avro:

dotnet add package Confluent.SchemaRegistry.Serdes.Avro --version 1.5.3
Enter fullscreen mode Exit fullscreen mode

Then copy the AVDL folder and Generate.ps1 script from the previous post.

The project should end up looking like:

MyProject
├── Avro
│   ├── AVDL
│   │   └── ExampleMessage.avdl
│   └── Generate.ps1
├── Class1.cs
└── MyProject.csproj
Enter fullscreen mode Exit fullscreen mode

Modify .csproj

Before building the .NET project, we want MSBuild to generate our Avro classes. This can be done as a PreBuild step which is configured within MyProject.csproj.

Inform MSBuild

Open the file then add the following:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
  <Exec WorkingDirectory="$(ProjectDir)Avro" Command="pwsh -ExecutionPolicy RemoteSigned -File Generate.ps1" />
  <ItemGroup>
    <Compile Include="$(ProjectDir)Avro/_generated/**/*$(DefaultLanguageSourceExtension)" Exclude="@(Compile)" />
  </ItemGroup>
</Target>
Enter fullscreen mode Exit fullscreen mode

This does a couple of things:

  • Execute the PowerShell script to generate Avro classes first
  • Inform MSBuild of the new files, but also ensure not to include them twice if they already exist (see here)

Tweak for Visual Studio

Also, add:

<ItemGroup>
  <EmbeddedResource Include="Avro\AVDL\**\*.avdl" />
</ItemGroup>
Enter fullscreen mode Exit fullscreen mode

This is a little hack to trick Visual Studio into marking the build as out of date if you make any changes in the AVDL folder.

Build

Now, we're ready to build:

dotnet build
Enter fullscreen mode Exit fullscreen mode

Which results in:

Microsoft (R) Build Engine version 16.7.0+7fb82e5b2 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /home/cainux/code/MyProject/MyProject.csproj (in 313 ms).
  Generating Avro classes...
  avro-tools not found, downloading from https://repo1.maven.org/maven2/org/apache/avro/avro-tools/1.10.0/avro-tools-1.10.0.jar to /home/cainux/.avro-tools/avro-tools-1.10.0.jar
  Running: /home/cainux/.dotnet/tools/avrogen -s ./_generated/avro/schema/ExampleMessage.avsc ./_generated/avro/classes
  MyProject -> /home/cainux/code/MyProject/bin/Debug/netstandard2.0/MyProject.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)
Enter fullscreen mode Exit fullscreen mode

Conclusion

We went from manually generating C# classes from Avro IDL files to making it happen automagically withing a .NET project. Developers only need to put their AVDL files in the right place and run build to generate the classes. Now you can focus on building the rest of your application with one less thing to think about.

If you're managing your code with source control (if not, why on earth not?!) such as Git - remember to ignore the _generated folder and its contents as they are build assets and shouldn't be checked in to source control.

Top comments (0)