Protocol buffers is a language-neutral serialization format for ideal structured data. You can consider XML or JSON, but the Protocol buffer is lighter and smaller. This format is widely used for data exchange between different Google systems.
Due to its optimal performance of structured data, protocol buffers is a good choice for data that represents the processing of restful services. To follow the rest principle, protocol buffers should be represented as a new hypermedia type. In the current version (. NET 4), Windows communication BASICS (WCF) include a new media type, which requires a considerable amount of effort. Fortunately, the new version of the wcf http stack makes the media-type WCF programming model the first-class citizen. You can go to the Glenn Block's blog to learn more details. I suggest you take a look at this book "Rest practice" http://book.douban.com/subject/6854551/
The following describes how to use Google protocol buffers to define only one hypermedia protobufferformatter:
Custom media types are serialized and deserialized by creating custom mediatypeformatter and onwritetostream () and onreadfromstream () methods. People often think that the media type is only used on the server side, but it is used to control the serialization and deserialization requirements on the client side, showing the role played by an HTTP request/response and media type formatting:
In this example, let's get started: build simple web API code and WCF web API preview 6. The media type used is application/X-protobuf. The core principle of the rest service is the loose coupling between the server and the client. The client needs to know the URI of the bookmarks, however, you should not know any other URI knowledge, but the client must know the link relationship.
The following code is a custom protobufferformatter. The constructor specifies the supported media type application/X-protobuf.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. net. http. formatting;
Using system. IO;
Using protobuf;
Using protobuf. Meta;
Namespace wcfwebformat. formatters
{
Public class protobufferformatter: mediatypeformatter
{
Public protobufferformatter ()
{
This. supportedmediatypes. Add (new system. net. http. headers. mediatypeheadervalue ("application/X-protobuf "));
}
Protected override void onwritetostream (type, object value, stream, system. net. http. headers. httpcontentheaders contentheaders, system. net. transportcontext context)
{
Serializer. serialize (stream, value );
}
Protected override object onreadfromstream (type, stream, system. net. http. headers. httpcontentheaders contentheaders)
{
Object OBJ = (runtimetypemodel. Default). deserialize (stream, null, type );
Return OBJ;
}
}
}
As shown above, we serialize the. NET object to the protobuf format in the onwritetostream method, and serialize the protobuf format to the. NET object in the onreadfromstream method.
Now we need to add protobuf serialized labels to our. Net object:
Using system. Collections. Generic;
Using system. xml. serialization;
Using protobuf;
Namespace contactmanager. Resources
{
[Protocontract]
Public class contact
{
[Protomember (1)]
Public int contactid {Get; set ;}
[Protomember (2)]
Public string name {Get; set ;}
}
}
Add protobufferformatter to the hypermedia set during the WCF runtime.
Using Microsoft. applicationserver. HTTP;
Using wcfwebformat. formatters;
Namespace contactmanager
{
Public class contactmanagerconfiguration: httpconfiguration
{
Public contactmanagerconfiguration ()
{
This. formatters. Add (New protobufferformatter ());
}
}
}
Modify the service configuration and use contactmanagerconfiguration:
VaR Config = new contactmanagerconfiguration () {enabletestclient = true };
Routes. Add (New serviceroute ("API/contacts", new httpservicehostfactory () {configuration = config}, typeof (contactsapi )));
The Code called on the client is as follows:
VaR serviceuri = new uri ("http: // localhost: 9000/API/contacts /");
VaR httpclient = new httpclient ();
Httpclient. baseaddress = serviceuri;
Httpclient. defaultrequestheaders. Accept. Add (new system. net. http. headers. mediatypewithqualityheadervalue ("application/X-protobuf "));
VaR response = httpclient. getasync ("1"). result;
Contact OBJ = (runtimetypemodel. Default). deserialize (response. content. readasstreamasync (). Result, null, typeof (contact) as contact;
VaR formatters = new mediatypeformattercollection () {New protobufferformatter ()};
VaR content = new objectcontent <contact> (OBJ, "application/X-protobuf", formatters );
Content. headers. contenttype = new mediatypeheadervalue ("application/X-protobuf ");
Httpclient. postasync (serviceuri, content );
Even though Google protocol buffers is not as popular as XML/JSON, protobuf is undoubtedly a very effective hypermedia type in the use of restful services. I wish you a pleasant New Year!
Related Articles:
- Data Exchange Protocol in Software System Development
- . Net built-in binary serialization, XML serialization and protobuf serialization compression comparison
- On Android, Gtalk and XMPP data of the push mechanism are in protobuf format rather than XML format.
- Leverage t4scaffolding for WCF web API
- Using-Protocol-Buffers-on-net-platform-part-I
- Using-Protocol-Buffers-on-net-platform-part-II
- Restful WCF/ef poco/unitofwork/Repository/MEF: 1 of 2