What is Protobuf?
It's pretty straightforward to serialize data over a Web API (or REST API) using Google Protocol Buffers (Protobuf). Despite being very verbose, slow to serialize, and lacking the features that Google provided to Protobuf, most developers prefer JSON as the go-to transfer protocol for services.
This could be primarily attributed to the fact that not many know the benefits of Protobuf Services, when to use them, and how to construct and consume them using ASP.NET Core and RestClient.Net.
This post covers all these details to help you make an informed decision for your next application.
Table of contents
- What is Protobuf?
- Why should you use Protobuf?
- Advantages of Protobuf
- Creating the service
- Consuming the service
- Creating Protobuf in C# using Visual studio
What is Protobuf?
Protocol buffers are a language-independent, platform-independent, and extensible method of serializing structured data. This is primarily used in communication protocols and data storage. Once you've defined how your data should be formed, you may utilize special generated source code to simply write and read structured data to and from a variety of data streams and languages.
You can even make changes to your data structure without affecting existing programs that were built using the "old" style. Based on Google's Protocol Buffers, Protobuf-net is a faster .NET serialization and deserialization library.
Why should you use Protobuf?
There are various types of serialization out there, but only a handful have Google's complete support behind them. Protobuf is swiftly overtaking JSON as the most popular serialization mechanism. It's even gaining support from Microsoft.
If familiarity is the most important criterion, Protobuf may not be the best option. Web developers have always been used to using JSON services in the past. They may not be fully aware of how to write Protobuf serialization code. Things are, however, changing. Protobuf is the logical choice when low latency and efficiency are priorities. Developers have started using it to implement services whenever possible. Who wouldn't want to save bandwidth and speed up their apps?
Advantages of Protobuf
Protobuf is a ‘programming language agnostic’ markup language that allows developers to define message types in .proto files. It means that payload model objects are automatically generated on both the server and client sides, saving time. It also compensates for the lack of human readability by ensuring message compatibility between client and server as long as the .proto is identical on both ends. Given that Protobuf is the foundation of gRPC, it's safe to infer that Google's interest in Protobuf is long-term. Other aspects of Protobuf include Reflection, which allows code to investigate the structure of messages, and any message types.
Creating the service
ASP.Net Core allows you to construct a gRPC service. However, developers may want to use a standard ASP.NET Core Web API that supports Protobuf serialization. Create a Web API project first, then install the gRPC NuGet packages.
These NuGets allow proto files to be used to define message kinds. Simply add a .proto file and modify the file's properties as shown below.
The .proto file can be included in a project that is shared across the client and server. This isn't always doable, but it's always a good idea. Otherwise, you can copy and paste the generated C# code into the client-side project.
There's a controller that takes Protobuf data via PUT and sends it back via GET. A message type is a person. It's available as part of the RestClient.Net samples. The service is immediately consumed by the unit tests. ToByteArray transforms the message into a byte array that may be sent over the network.
Consuming the service
RestClient.Net allows any.NET application to consume the service. To begin, download and install the RestClient.Net NuGet package, as well as the gRPC and Protobuf packages. This will work with projects built using the.NET Framework,.NET Core, UWP, and Xamarin. An example from the RestClient.Net .NET Core sample is shown below. It sends a Protobuf binary person to the server and then retrieves that person.
Creating Protobuf in C# using Visual studio
Follow the below steps to create a simple Protobuf in C# through Visual Studio -
- Create a console application in.NET Core.
- Add the Core, Core.Tools, and Google NuGet packages to your project.
- Add a new file named "Person.proto" to the project by right-clicking on it.
- In the "Person.proto" file, add the following -
The syntactic version of the proto file is proto3. If we don't mention this, proto2 will be used as the default syntax version.
The csharp_namespace line is optional. All classes will be created under this namespace when code is generated by the proto3 compiler.
‘message Person’ and ‘Address’ are two types of data structures. Person and Address are two separate classes that will be formed when code is generated by the Protobuf compiler. The fields id, name, Address, Id, AddressLine1, and city will be present in both
Each field must always be given a unique identification. The reason for giving a unique identification is that when the compiler serializes or deserializes any field, it will do so using this unique identifier, resulting in speedier serialization and deserialization.
Here are the final steps to generate the code -
- Right-click the proto file and select Properties from the drop-down menu. From the build action's choices, select "Protobuf compiler." Your proto file will be compiled by the Protobuf compiler if this property is set. If you don't have the above NuGet packages installed, you won't be able to see the "Protobuf compiler" option.
- Create a solution.
- Open obj/debug/netcoreapp/Message.cs after a successful build. The Protobuf compiler generates this code automatically.
Using this post as a reference, you can add, alter, or remove message types from the .proto file. ASP.NET Core provides an excellent foundation for Protobuf and C# service delivery. You can also create your own Protobuf in C# and experiment with it.