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