https://github.com/WebApiContrib

ASP.NET Web API and Protocol Buffers

Protocol Buffers are a super efficient and very flexible way of serializing structured data. Developed at Google, provide the developers lightspeed serialization and deserialization capabilities.

There a handful of .NET implementations, the most popular one, called protobuf-net, created by Marc Gravell from StackExchange. On top of that library, WebApiContrib project has a Web API Protocol Buffers formatter ready for you to plug in.

More after the jump.

Why?

Protocol Buffers can give you a performance boost up to serveral orders of magnitude better than other serializers, such as DataContractSerializer or JSON.NET. Demis Bellot wrote a nice comparison of these, where you can see how Protocol Buffers stacks against other.

Another advantage, in addition to speed, is that it’s incredibly dense (provides a minimal output), making it very suitable for sending over the wire. It is not human readable (binary) and the schema is required to correctly deserialize a message, so that makes it a bit problematic for public use, but as far as I am concerned, most (if not all) internal APIs should use Protocol Buffers for performance reasons.

Installation

The media type formatter for Protocol Buffers has actually been available as part of WebApiContrib (written for the project by Christian Weyer) for a while now. This weekend we have made a few changes to improve it and give you more flexibility.

As you might expect, you can install the formatter from Nuget:

 
 
 
 
 

Shell

 
1
PM> Install-Package WebApiContrib.Formatting.ProtoBuf

If you wish to have a look at the source, that’s obviously available at WebApiContrib Github pages.

Using Protocol Buffers with Web API

For starters you need to register the formatter against your Web API configuration, at application startup:

 
 
 
 
 

C#

 
1
config.Formatters.Add(new ProtoBufFormatter());

The registered media type by default is application/x-protobuf. The types that you wan to return serialized to protobuf format need to be decorated either with protobuf=net specific attributes (note order is necessary to specify):

 
 
 
 
 

C#

 
1
2
3
4
5
6
7
8
9
10
    [ProtoContract]
    public class Item
    {
        [ProtoMember(1)]
        public int Id { get; set; }
        [ProtoMember(2)]
        public string Name { get; set; }
        [ProtoMember(3)]
        public long Value { get; set; }
    }

Or with regular DataContract attributes.

 
 
 
 
 

C#

 
1
2
3
4
5
6
7
8
9
10
    [DataContract]
    public class Item
    {
        [DataMember(Order = 1)]
        public int Id { get; set; }
        [DataMember(Order = 2)]
        public string Name { get; set; }
        [DataMember(Order = 3)]
        public long Value { get; set; }
    }

The latter approach is recommended since the behavior defined by them can be picked up by other Web API formatters such as JSON and XML. In this case, just like before you must specify the order to get a reliable key number and guarantee the data integrity!

This is enough to get you started – so you can now request your API with this accept headers and that would produce protobuf output.

By default, protobuf-net uses UseImplicitZeroDefaults set to true – this means it will omit properties with default values (not set explicitly). Since this can omit things like bool and int, for this particular Web API implementation, we reset it to false arbitrarily so that you don’t run into unexpected issues, such as missing properties.

However, this is not everything. It is known the first (few) serializations with protobuf-net can be slow. If you are seeking really extreme performance, you can precompile specific types. ProtoBufFormatter exposes a static property Model (of protobuf-net RuntimeTypeModel type) which is intended to be a hook for you.

For example:

 
 
 
 
 

C#

 
1
2
ProtoBufFormatter.Model.Add(typeof (Item), true);
ProtoBufFormatter.Model.CompileInPlace();

This is not necessary but will give you performance improvement during first requests.

All in all – it’s a big performance boost, and the formatter is ready to be plugged in – so go ahead and boost your API performance!

PS: If you have any issues, make sure to file a bug!

Be Sociable, Share!

WebApiContrib的更多相关文章

  1. Vue.js——基于$.ajax实现数据的跨域增删查改

    概述 之前我们学习了Vue.js的一些基础知识,以及如何开发一个组件,然而那些示例的数据都是local的.在实际的应用中,几乎90%的数据是来源于服务端的,前端和服务端之间的数据交互一般是通过ajax ...

  2. ASP.NET Web API 配置 JSONP

    之前的一篇博文:jsonp跨域+ashx(示例) 1. 安装 Jsonp 程序集: PM> Install-Package WebApiContrib.Formatting.Jsonp PM&g ...

  3. Nancy之实现API的功能

    0x01.前言 现阶段,用来实现API的可能大部分用的是ASP.NET Web API或者是ASP.NET MVC,毕竟是微软官方出产的,用的人也多. 但是呢,NancyFx也是一个很不错的选择.毕竟 ...

  4. 在Web Api中集成protobuf

    安装WebApiContrib.Formatting.ProtoBuf Install-Package WebApiContrib.Formatting.ProtoBuf 注册ProtoBufForm ...

  5. 控制ASP.NET Web API 调用频率

    很多的api,例如GitHub’s API 都有流量控制的做法.使用速率限制,以防止在很短的时间量客户端向你的api发出太多的请求.例如,我们可以限制匿名API客户端每小时最多60个请求,而我们可以让 ...

  6. 让Web API支持Protocol Buffers

    简介 现在我们Web API项目基本上都是使用的Json作为通信的格式,随着移动互联网的兴起,Web API不仅其他系统可以使用,手机端也可以使用,但是手机端也有相对特殊的地方,网络通信除了wifi, ...

  7. asp.net core 使用protobuf

    在一些性能要求很高的应用中,使用protocol buffer序列化,优于Json.而且protocol buffer向后兼容的能力比较好. 由于Asp.net core 采用了全新的MiddleWa ...

  8. asp.net WebApi and protobuff

    protobuff 是谷歌开发的,在性能上要比Json xml好很多,对性能要求比较高的时候这个是一个不错的选择,但是这个目前只是一个序列化反序列化的东西,以前原生的只有几种语言的现在在github ...

  9. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【四】——实现模型工厂,依赖注入以及格式配置

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在上一篇中,我们已经初步开始使用Web Api了,但同时出现了一些很多不足之处,本章我们就着 ...

随机推荐

  1. MyEclipse—怎样在MyEclipse中创建servlet3.0

    servlet3.0随着Java EE6规范一起公布,那么怎样在MyEclipse中创建3.0版的servlet呢? 1.启动MyEclipse.获得下图: 2.点击上图"File" ...

  2. javascript变量,作用域和内存问题(一)

          js对象的引用是很有意思的,引用型对象是不可以直接引用的,我猜测这是原型的来源之一,有大神请详解或斧正.    “引用类型的值是保存在内存中的对象.与其他语言不同,JavaScript不允 ...

  3. nodejs 模拟form表单上传文件

    使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...

  4. Windows命令行命令集锦

    原文:Windows命令行命令集锦 转自:http://www.me2wg.com/bbs/forum.php?mod=viewthread&tid=15830 winver--------- ...

  5. 走向DBA[MSSQL篇] 从SQL语句的角度 提高数据库的访问性能

    原文:走向DBA[MSSQL篇] 从SQL语句的角度 提高数据库的访问性能 最近公司来一个非常虎的dba  10几年的经验 这里就称之为蔡老师吧 在征得我们蔡老同意的前提下  我们来分享一下蔡老给我们 ...

  6. [SignalR]异常信息捕获以及处理

    原文:[SignalR]异常信息捕获以及处理 异常处理,一般采用try..catch方式处理,而signalR里面有HubPipelineModule类可以捕获到Hub内发生的异常信息. 从上图中,可 ...

  7. VC和gcc在保证功能static对线程安全的差异变量

    VC和gcc不同,不能保证静态变量的线程安全性.这就给我们的程序带来了非常大的安全隐患和诸多不便.这一点应该引起我们的重视!尤其是在构造函数耗时比較长的时候.非常可能给程序带来意想不到的结果.本文从測 ...

  8. Linux Howto

    1. Customize the Xfce menu http://wiki.xfce.org/howto/customize-menu

  9. HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划)

    1506意甲冠军:给你一个连续的直方图(拼贴底部长度1).求连续基质区. 对每一个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h ...

  10. 第三十 访问财富进退自如 —Spring交易管理

    6月16日本,明确. "应该留给追穷寇勇,不可沽名学霸王.天若有情天亦老,人间正道是沧桑." 有始有终.有往有还.进退自如乃Spring事务管理之道,也是万物生生不息.和谐共处之道 ...