. Net Thrift Tour (1) Windows Installation and HelloWorld, thrithelloworld

Source: Internet
Author: User

. 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:

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.