. NET Thrift Tour (i) Windows installation and HelloWorld

Source: Internet
Author: User

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

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.