.NET

Working with Protobuf WCF Services

By Joydip Kanjilal, August 06, 2013

A fast, platform- and language-independent format for exchanging serialized data

Protocol Buffers are a serialization format, developed by Google, that is fast and easy to use. Frequently referred to as "Protobuf," the technology consists of a language- and platform-neutral, extensible, binary format for fast data exchange that relies on an interface description language, which is compiled to native code. This article explains how to work with Protobuf technology. In addition, I'll show how to pair Protobuf with Windows Communication Foundation (WCF), which is Microsoft’s unified programming model for building service-oriented apps. And I'll demonstrate the benefits of using Protobuf this way.

To work with the code examples listed in this article, you need to have:

Visual Studio 2010

Protocol Buffers for .NET (Protobuf-net)

You can freely download a copy of Protocol Buffers for .NET. Before we delve deep into implementing Protobuf WCF services in .NET, let's take a quick tour of the basics of Protobuf services: What are they and how are they used.

Protocol Buffers

The idea behing is Protocol Buffers to provide a platform- and language-independent format for exchanging serialized, structured data. Protocol Buffers are lighter and faster to handle than XML or JSON. The other major advantages of Protocol Buffers are:

Reduced size of the data packets

Platform independence

Extensibility

Creating Protobuf Objects in Visual Studio

Note that all protobuf-net classes are stored in files that have .proto extensions. You can store one or more proto classes in a single .proto file. Visual Studio 2008 and 2010 now have support for creating protobuf-net classes through a custom tool. You can find more information on how the tool works.

You are now ready to create .proto files from Visual Studio. You can now use the Protocol Buffers template  you'll see it in the list of installed templates in your Visual Studio Add New Item dialog as shown in Figure 1.

Figure 1: Creating Protocol Buffer objects in Visual Studio.

Protobuf Messages

To create a Protcol Buffer class (known as a "message" in protobuf), select Add | New Item in the solution explorer window, then select Protocol Buffers from the list of the templates displayed, as in Figure 1.

Notice the use of the required attribute in the User message shown above. This qualifier indicates that the attribute is mandatory. The optional attributes are used to indicate that the property may or may not contain data. When creating protobuf-net data classes, each of the properties of the class should be explicitly numbered and have a proper ordering.

You would use the repeated attribute to indicate that the property can contain a list of objects of the same type. For example: Suppose we need to store a history of a user's passwords. To do this, we would need a list. Refer to the PasswordHistory message as shown below:

Once you create and use your .proto file in Visual Studio, the protogen compiler automatically generates the code-behind file for your proto message. Here's how a typical automatically generated class looks:

You can also specify that your WCF service should use protobuf-net in the configuration file; more on this later.

Let's implement a sample application that makes use of Protobuf WCF services. To do this, follow these steps:

Open Visual Studio IDE

Click on File | New | Project

Select WCF Service Application from the list of the project types

Save the WCF Service application project with a name

Create data contract classes for this service

Create a service contract and a service implementation class for this service

Repeat steps 3 and 4 to create another WCF Service application with the same implementation

We'll use the DDJ.Services project shown in Figure 2 for our sample: This is a WCF Service application that makes use of Protobuf technology. The other WCF Service aApplication project (Test.Services) contains the same implementation, but doesn't use Protobuf. I have done this so that we can compare the performance of a Protobuf WCF service against a WCF service that has the same implementation, but doesn't employ Protobuf.

Implementing the Data Contracts

In the DDJ.Services project, I use the following data contract classes:

AuthenticateUserRequest

AuthenticateUserResponse

User

The AuthenticateUserRequest class contains the properties used to authenticate a user. The source for this class is:

The AuthenticateUser operation contract returns an instance of the AuthenticateUserResponse class. The properties of this class include: StatusCode, StatusMessage, and a Boolean property called IsAuthenticated that is set to true or false based on whether the user is authenticated. The AuthenticateUserResponse class follows:

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!