zoukankan      html  css  js  c++  java
  • WebApiContrib

    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:

    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:

    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):

    Or with regular DataContract attributes.

    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:

    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!
  • 相关阅读:
    天地图专题六:复杂操作,天地图上标注点的连线以及模拟点击事件
    天地图专题五:在天地图上绘制电子区域并保存数据
    天地图专题四:在天地图上显示运行轨迹
    天地图专题三:根据标注点的范围确定天地图的中心点和缩放级别
    天地图专题二:在天地图上循环显示标注点以及悬停显示信息窗口
    天地图专题一:加载天地图
    从百度地图批量获取中国县级以上行政区划边界坐标
    【百度地图API】如何获取行政区域的边界? (转载)
    高德地图api之location定位
    url中的空格
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4229173.html
Copyright © 2011-2022 走看看