Click here to Skip to main content
13,198,850 members (41,136 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 2 Feb 2013

WebAPI Follow-up... What About Protocol Buffers?

, 3 Feb 2013
Rate this:
Please Sign up or sign in to vote.
As a follow-up to my previous article about WebAPI, I received an interesting question from a friend on LinkedIn: Is there a way to emit data in Protobuf format?

As a follow-up to my previous article about WebAPI, I received an interesting question from a friend on LinkedIn: Is there a way to emit data in Protobuf format?  

This one got me thinking... ProtoBuf is short for Protocol Buffers which is a data format technology invented by Google to allow for maximum compression of structured data that can be interpreted by any programming language. Google says: "think XML, but smaller, simpler, and FASTER", and I think they may be right. According to Google's own benchmarks, Protocol Buffer format is 3-10x smaller and 20-100x faster on the wire.

Fortunately, there is a pair of NuGet packages available to help our WebAPI application handle the protocol buffer format. From within my WebAPI application, I can use the NuGet console to install these two packages with the following command:

Install-Package WebApiContrib.Formatting.ProtoBuf

Once this command completes, the protocol buffers library protobuf-net should be installed as well as the appropriate media formatter in the WebApiContrib.Formatting.ProtoBuf library. The next step to configure WebAPI is to configure the application to be aware of the new formatter. This is accomplished with the following command in Application_Start in the global.asax.cs file:

protected void Application_Start(object sender, EventArgs e)

		new WebApiContrib.Formatting.ProtoBufFormatter());


Finally, we need to configure the data we are going to transport so that the Protocol Buffers library knows how to serialize it. This is accomplished with a series of attributes on our Team class:

public class Team
    public string City { get; set; }

    public string Name { get; set; }

The ProtoContract attribute tells the serializer that this class can be formatted with protocol buffers. The two ProtoMember attributes assign a location to the properties in the resultant stream of data. The numeric locations identifiers should be continuous and start with one.

With this minor bit of configuration, we can start our application and open Fiddler to easily test our new protocol buffer aware API. If I point Fiddler to the standard location http://localhost:4829/, I'll get normal JSON output:

{"City":"Boston","Name":"Red Sox"},
{"City":"San Diego","Name":"Chargers"}

but if I go into the Composer tab of Fiddler and submit the request with an accept header of application/x-protobuf:

Fiddler Composer - Sending a GET request for Protocol Buffer content

The results returned are the protocol buffer format, not entirely visible to us as text:

Protocol Buffer Formatted ResultSet

We didn't change any of our business logic. We didn't change any of our data access code. By simply adding an additional media formatter, WebAPI was able to handle and format the resultset as requested. What else could you format with WebAPI?  What other request formats could you interact with?  Let me know what you think in the comments below.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Jeffrey T. Fritz
Program Manager
United States United States
Jeffrey is a software developer coach, architect, and speaker in the Microsoft.Net community. He currently works as a program manager for the Microsoft .NET Developer Outreach group. He has delivered training videos on Pluralsight, WintellectNow, and on YouTube. Jeffrey makes regular appearances delivering keynotes, workshops, and breakout sessions at conferences such as TechEd, Ignite, DevIntersection, CodeStock, FalafelCon, VSLive as well as user group meetings in an effort to grow the next generation of software developers.

You may also be interested in...


Comments and Discussions

QuestionHow we can call that web api in my c# project. Pin
Member 1215180319-May-17 2:19
memberMember 1215180319-May-17 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 3 Feb 2013
Article Copyright 2013 by Jeffrey T. Fritz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid