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