Protocol Buffers is language neutral format for serializing structured data in a very optimal format. You can think about protocol buffers as XML or JSON but lighter and smaller. This format its widely used at Google to exchange data between different systems.
Given its optimal representation of structured data, protocol buffers it’s a great option for representing data processed by RESTful services. To follow the principles of REST, protocol buffers should be a represented as a new hypermedia type. In the current version of Windows Communication Foundation (WCF), incorporating a new media type requires a significant amount of effort. Fortunately, the new version of the WCF HTTP stack makes media type a first class citizen of the WCF programming model. You can find more details about this new stack on Glenn Block’s weblog.
The following section will illustrate how to leverage Google’s protocol buffers as a native media type in the WCF stack.
Implementing a protocol buffers media type processor with WCF-HTTP
The new WCF HTTP programming model abstracts the processing of a hypermedia type by using a component known as media type processors. Essentially, media type processors are represented by classes that inherit from the MediaTypeProcessor class and are invoked by the WCF runtime to handle the serialization incoming and outgoing messages in a specific media type representation.
The first important thing we need to figure out in order to implement a protocol buffers media type processors is the correct API to use. From the existing implementations, I believe the Marc Gravell’s implementation provides the cleanest programming model. Serializing an object into protocol buffers is as simple as illustrated in the following code.
We can leverage the same technique to implement a protocol buffers media type processor as highlighted in the following code:
As illustrated above, the WriteToStream operation handles the serialization of the .NET objects into the protocol buffers format.
Let’s explore how to use our media type processor with the following WCF service:
We can inject the protocol buffers media processor into the WCF runtime by implementing the following host configuration as illustrated in the following code.
We can initialize the host configuration in the route tables structure as shown in the following code.
At this point, our WCF service is ready to leverage the protocol buffers hypermedia format. The client should indicate the expected media type by using the Accept HTTP header as shown in the following code.
GET /ProtocolBuffers/accounts">/ProtocolBuffers/accounts">/ProtocolBuffers/accounts">http://<service endpoint>/ProtocolBuffers/accounts HTTP/1.1
The response message using the protocol buffers format looks like the following:
HTTP/1.1 200 OK
Date: Fri, 21 Jan 2011 04:15:32 GMT
Even though Google’s protocol buffer is nowhere near in popularity as other serialization formats such as XML or JSON it is, undoubtedly, an very efficient hypermedia type to use in your RESTful services.