DEV Community

adams.NET
adams.NET

Posted on

How to enable a binary message format in your ASP.NET Core Web APIs

As for web APIs, JSON is ubiquitous nowadays but in some circumstances we may want to reduce the network traffic as much as possible. In such cases hardly anything beats a binary message format. So, usually I use Protobuf (to be more precise, protobuf-net) for this purpose as it is a well-established data exchange format with multi-platform support.

To add some binary serialization capability to your web API project, first you'll need an InputFormatter/OutputFormatter implementation for the chosen format. In my ASP.NET Core template project I have one for protobuf-net.

After copying this over to your project and doing a bit of configuration in your Startup class, you're good to go: just pass application/x-protobuf in the Content-Type and Accept HTTP headers when you invoke your API and the bits will come and go over the wire in a sexy binary format right away.

One more thing to remember: of course, your models need to be properly decorated with attributes (preferably with DataContract/DataMember attributes to leave the door open for other serialization formatters) to get protobuf-net going. But that's all indeed!

Top comments (0)