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!
  • 相关阅读:
    SQL Server TSQL高级查询
    ado.net
    Apache配置详解(最好的APACHE配置教程)
    CrystalReport for vs2010 水晶报表的发布问题以及捆绑发布
    Rails 疑难解决
    [转]if 命令示例详解
    How To Deploy on Bluehost
    CustomActionData 属性 [Visual Studio 中的部署]
    BlueHost下部署rails app经验
    Using Ruby On Rails on Bluehost
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4229173.html
Copyright © 2011-2022 走看看