. Net Thrift Tour (1) Windows Installation and HelloWorld, thrithelloworld
1. Download Thrift
Http://apache.fayea.com/apache-mirror/thrift/0.9.2/thrift-0.9.2.tar.gz
2. Download the compiler and decompress it.
Http://www.apache.org/dyn/closer.cgi? Path =/thrift/0.9.2/thrift-0.9.2.exe
3. Set the compiler path to an environment variable.
My thrift-0.9.2.exe is placed in the C:/Windows path, so set path in the environment variable to C: \ Windows;
4. Compile 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 based on thrift
In cmd, specify the path to test. Thrift and run the thrift -- gen csharp test. thrift command.
The gen-csharp folder is automatically generated, which contains two files: User. cs and UserService. cs.
6. Create a project
Add the automatically generated file to MyInterface
MyServer
Class Program {static void Main (string [] args) {TServerSocket serverTransport = new TServerSocket (8899, 0, false); UserService. processor processor = new UserService. processor (new MyUserService (); TServer server = new TSimpleServer (processor, serverTransport); Console. writeLine ("Starting server on port 8899... "); server. serve () ;}} public class MyUserService: UserService. iface {public User GetUserByID (int userID) {return new User () {ID = 1, Name = "wangxm" };} public List <User> GetAllUser () {List <User> users = new List <User> () {new User () {ID = 1, Name = "wangxm"}, new User () {ID = 2, name = "xxxx" }}; return users ;}}View Code
MyClient
Static void Main (string [] args) {TTransport transport = new TSocket ("localhost", 8899); TProtocol protocol = new TBinaryProtocol (transport); UserService. client client = new UserService. client (protocol); transport. open (); var users = client. getAllUser (); users. forEach (u => Console. writeLine (string. format ("User ID: {0}, User Name {1}", u. ID, u. name); var user = 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 running result is as follows: