Integrate protobuf and apiprotobuf in Web APIs

Source: Internet
Author: User

Integrate protobuf and apiprotobuf in Web APIs

  • Register ProtoBufFormatter
  • Set DTO
  • Client call
  • Static void Main (string [] args) {var httpClient = new HttpClient (); httpClient. defaultRequestHeaders. accept. add (new System. net. http. headers. mediaTypeWithQualityHeaderValue ("application/x-protobuf"); var response = httpClient. getAsync (" http://localhost:60339/api/Default/GetItem "). Result; // deserialize ProtoBuf Stream into a set var obj = (RuntimeTypeModel. default ). deserialize (response. content. readAsStreamAsync (). result, null, typeof (List <Item>) as List <Item>; Console. writeLine (obj. count); // set the request header var content = new ObjectContent <List <Item> (obj, new ProtoBufFormatter (); content. headers. contentType = new MediaTypeHeaderValue ("application/x-protobuf"); var postResponse1 = httpClient. postAsync (" http://localhost:60339/api/Default/PostItem ", Content ). result; var postResult = postResponse1.Content. readAsStreamAsync (). result; var intValue = (RuntimeTypeModel. default ). deserialize (postResult, null, typeof (int); Console. writeLine (intValue); Console. readKey ();}

    Use Fiddler to send data requests in Json, Xml, and Protobuf formats.

     

     

    Git

    Https://github.com/xlb378917466/WebApi_protobuf

     

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.