Skip to the content.

Working with .proto files with protobuf-net and protobuf-net.BuildTools

protobuf-net originated as a code-first tool, but sometimes you’re working with contract-first .proto files instead, whether because the schemas are supplied externally, or because you want to ensure that your schemas are as suitable as they can be for cross-platform usage.

protobuf-net.BuildTools is the most convenient way to integrate .proto files with your build process, using the SDK from .NET 5 and the new “generators” feature of Roslyn.

All you need to do is:

Now, when you build your project - whether in the IDE, or at the command-line - the protobuf-net.BuildTools utility generates the C# for your types on the fly, and generates the required code - without you needing to worry about how to configure it. For example:

So you just use the types as though they existed - they will show in the IDE with all the expected properties etc, just as if they were C# files. Because they now are.

A range of common import files (for example all of the google/protobuf/*.proto files) are included in the utility as resources, and do not need to be included in your project unless you want to change the version/contents from the copy baked into the tool. Normally, standard imports should just work.

Additional options

Additional configuration options can be specified as attributes against each <AdditionalFiles> node, to fine tune your options; this is very similar to the options available with the command-line tools:

That doesn’t work for you?

protobuf-net also provides:

All of these tools can be awkward, brittle, and non-obvious.