C # Protobuf Getting Started

Source: Internet
Author: User

Personal understanding: Protobuf is a protocol for transmitting data, or a format, similar to JSON.

Start by listing the tools you need:

VS2015
Protobuf-csharp-port-master:https://github.com/jskeet/protobuf-csharp-port
(Note: There is another tool protobuf-net more convenient to use, interested can refer to this article: http://www.cnblogs.com/jhli/p/6139914.html)

First, unzip the downloaded Protobuf-csharp-port-master tool and go to the build folder

Click Buildall.bat to automatically generate Build_output and build_temp two new folders under sub-folders

Create a new Test.proto file

Package Prototest;message Person {  Required String name = 1;  Required Int32 id = 2;        Unique ID number for the this person.  Optional String email = 3;   Enum Phonetype {    MOBILE = 0;    HOME = 1;    Work = 2;  }   Message PhoneNumber {    Required String number = 1;    Optional Phonetype type = 2 [default = HOME];  }   Repeated PhoneNumber phone = 4;}

Put it in the Build_output Tools folder

Run command Protoc--descriptor_set_out=test.protobin--include_imports Test.proto will be reborn as a Test.protobin file under the folder

Then run Protogen test.protobin to generate the Test.cs file.

The CS file can then be added directly to the project using the

As an example:

Create a new WebService project

Add method

[WebMethod]          Public byte [] Test ()        {            returnnew Person.builder (). SetId (1). SetName (" Wang Nima "). Setemail ("[email protected]"). Build (). Tobytearray ();        }

Call this method in another project

New protoservicesoapclient ();             var test = service. Test ();             = ProtoTest.Person.ParseFrom (test);

You can see that this data has been transmitted to the caller.

C # Protobuf Getting Started

Related Article

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.