1. Download Thrift
Http://apache.fayea.com/apache-mirror/thrift/0.9.2/thrift-0.9.2.tar.gz
2. Download the compiler and unzip it
Http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.exe
3. Set the compiler's path to the environment variable
My thrift-0.9.2.exe is placed under the C:/windows path, so set the path in the environment variable: C:\Windows;
4. Write a Test.thrift file
namespace Java com.javabloger.gen.code struct User { 1: i32 ID 2: string Name } Service UserService { User Getuserbyid (1: i32 userID) list<User> getalluser () }
5. Automatically generate CSharp code according to thrift
CMD in the path specified to test. Thrift path, execute command thrift--gen csharp Test.thrift
The Gen-csharp folder is automatically generated with 2 files User.cs and UserService.cs
6. New Project
Add a newly generated file to the MyInterface
MyServer
classProgram {Static voidMain (string[] args) {Tserversocket Servertransport=NewTserversocket (8899,0,false); Userservice.processor Processor=NewUserservice.processor (NewMyuserservice ()); Tserver Server=NewTsimpleserver (processor, servertransport); Console.WriteLine ("starting server on port 8899 ..."); Server. Serve (); } } Public classMyUserService:UserService.Iface { PublicUser Getuserbyid (intUserID) { return NewUser () {ID =1, Name ="WANGXM" }; } PublicList<user>Getalluser () {List<User> users =NewList<user>(){ NewUser () {ID =1, Name ="WANGXM" }, NewUser () {ID =2, Name ="xxxx" } }; returnusers; } }
View Code
MyClient
Static voidMain (string[] args) {Ttransport Transport=NewTsocket ("localhost",8899); Tprotocol Protocol=NewTbinaryprotocol (transport); Userservice.client Client=Newuserservice.client (protocol); Transport. Open (); varUsers =client. Getalluser (); Users. ForEach (U= Console.WriteLine (string. Format ("user ID: {0}, user Name {1}", U.id, u.name)); varuser = client. Getuserbyid (1); Console.WriteLine ("------------------"); Console.WriteLine (string. Format ("user ID: {0}, user Name {1}", user.id, user. Name)); Console.ReadLine (); }
View Code
7: The result of the operation is as follows
. NET Thrift Tour (i) Windows installation and HelloWorld