ASP. WebApi and Protobuff

Source: Internet
Author: User

Protobuff is developed by Google, in performance than JSON XML is much better, high performance requirements when this is a good choice, but this is currently just a serialization of the deserialization of things, formerly native only several languages now on GitHub There are many languages available on your own to view.

The Protobuff is integrated here, you can write a format or you can install a third-party package. If it is to write their own inheritance mediatypeformatter, here I use a third-party package roughly said how to use. Install Webapicontrib First. Formatting. Protobuf in the Package Manager, and then register me in Webapiconfig. Here's the code.

  Config. Formatters.insert (0new protobufformatter ());

This is a change to the serialization mode, or you can set the specified type this time the code is as follows

ProtoBufFormatter.Model.Add (typeoftrue); ProtoBufFormatter.Model.CompileInPlace ();

Then add an entity contact here code is as follows:

[Protocontract] Public classContact {[Protomember (1)]         Public intId {Get;Set; } [Protomember (2)]         Public stringFirstName {Get;Set; } [Protomember (3)]         Public stringLastName {Get;Set; } [Protomember (4)]         Public stringAddress {Get;Set; } }

Create a new controller. This is restfull style.

 

  Public classTestcontroller:apicontroller { PublicIenumerable<contact>Get () {varList =NewList<contact>            {                NewContact () {id=123456789, firstname="T", lastname="L", address="Front.ltd"},                           }; List. ADD (NewContact () {Id =987654321, FirstName ="x", LastName ="a", Address ="Front.ltd" }); List. ADD (NewContact () {Id =330987260, FirstName ="D", LastName ="x", Address ="Front.ltd" }); returnlist; }                              //POST api/<controller>         Public voidPost ([frombody]contact value) {Console.WriteLine ("---"); Console.WriteLine (value.            ToString ()); Debug.WriteLine ("Has receive the data");        Debug.WriteLine (value); }        //PUT API/<CONTROLLER>/5         Public voidPut (intID, [frombody]stringvalue) {        }        //DELETE API/<CONTROLLER>/5         Public voidDelete (intID) {}

This time the client will be able to access it this way.

Debug.WriteLine ("Start"); varServiceuri =NewUri ("http://localhost:12706/api/"); varClient =NewHttpClient {baseaddress =NewUri ("http://localhost:12706/api/Test/") }; Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (NewMediatypewithqualityheadervalue ("Application/x-protobuf")); varResponse = client. Getasync ("test/").                      Result; varR =Response.            Content.readasstreamasync (); if(r.iscompleted) {varCon=deserialize<list<contact>>(R.result); Con. ForEach (Model={Console.WriteLine ($"The first name is {model. FirstName} The Address:{model. Address}");                        }); 

I am here in the console running, but also in the JS and other programs to visit, this article on the Internet a lot of it is not here to say. Just got to get if that's the post code is like this

 varContractmodel =NewContact (); Contractmodel.firstname="Xiao"; Contractmodel.address="Front.ltd"; varContentrequest =NewStringcontent (serialize<contact>(Contractmodel)); ContentRequest.Headers.ContentType=NewMediatypeheadervalue ("Application/x-protobuf");            Debug.WriteLine (contentrequest); varres = client. Postasync ("test/", contentrequest); if(Res. iscompleted) {Console.WriteLine (res).            Result); }

This is the whole process, if anyone needs this demo can leave a message. In the integration of the time encountered some problems, this protocontract tag with the Protobuff-net tool generated by the code is not the same, can not be mixed. It is best to create a new entity class that is a two-part client and server. The other two links are referenced here, because the situation is not quite the same, it took a lot of time to debug.

RELATED links:

http://www.cnblogs.com/shanyou/archive/2012/01/22/ Using-google-protocol-buffers-hypermedia-type-with-wcf-restful-services-a-media-type.html

http://www.strathweb.com/2013/02/asp-net-web-api-and-protocol-buffers/

ASP. WebApi and Protobuff

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.