1. 新建一个目录,C:\test\thrift-test,里面建2个子文件夹,client-node和sever-csharp,然后把Thrift官方的thrift定义文件也拷贝进去。
2. 官方的thrift定义文件,会去引用一个shared的thrift文件,但是这个文件貌似没地方下载,导致生成接口时报错,索性我就把他从定义里面删掉,干净起见,注释也删了。
namespace cpp tutorial
namespace d tutorial
namespace java tutorial
namespace php tutorial
namespace perl tutorial
typedef i32 MyInteger
const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
struct Work {
1: i32 num1 = 0,
2: i32 num2,
3: Operation op,
4: optional string comment,
}
exception InvalidOperation {
1: i32 what,
2: string why
}
service Calculator{
void ping(),
i32 add(1:i32 num1, 2:i32 num2),
i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),
oneway void zip()
}
3. 打开Visual Studio 2012,新建一个项目,名为thrift-1stApp-server,项目放在刚才建立的子目录server-csharp下。
4. 右键项目,选择Manage Nuget Packages,选择Online, 然后搜索thrift,第一个应该就是Apache官方提供的thrift C#库了,注意版本号和CreateBy属性,CreateBy一定要是The Apache Software Foundation,别下载山寨版。版本号要和接下来下载的thrift compiler对应。我这里显示的是2012年11月20日发布的0.9.0.0版本。点击安装,thrift.dll就被自动加入到项目引用里面了。
5. 现在thrift的windows版compiler(已经帮你编译好的exe),官方提供的下载链接是:http://thrift.apache.org/download/,只有最新0.91版的,找了半天也没找到0.90的,网上搜了下,总算下载到了:http://archive.apache.org/dist/thrift/0.9.0/
6. 把下载下来的thrift-0.9.0.exe,放到D:\Tools\thrift目录下。
7. 执行: C:\test\thrift-test\server-csharp\>D:\Tools\thrift\thrift-0.9.0 --gen csharp ..\tutorial-tutorial.thrift
8. 你应该能看到,多出来一个子目录gen-csharp,把该目录下的所有文件都Include到Visual Studio的工程里面去。
9. 编译一下,应该是没有编译错误的。
1. 添加CalculatorHandler.cs:
using System;
namespace thrift_1stApp_server
{
public class CalculatorHandler : Calculator.Iface
{
public CalculatorHandler()
{
}
public void ping()
{
Console.WriteLine("ping()");
}
public int add(int n1, int n2)
{
Console.WriteLine("add({0},{1})", n1, n2);
return n1 + n2;
}
public int calculate(int logid, Work work)
{
Console.WriteLine("calculate({0}, [{1},{2},{3}])", logid, work.Op, work.Num1, work.Num2);
int val = 0;
switch (work.Op)
{
case Operation.ADD:
val = work.Num1 + work.Num2;
break;
case Operation.SUBTRACT:
val = work.Num1 - work.Num2;
break;
case Operation.MULTIPLY:
val = work.Num1 * work.Num2;
break;
case Operation.DIVIDE:
if (work.Num2 == 0)
{
InvalidOperation io = new InvalidOperation();
io.What = (int)work.Op;
io.Why = "Cannot divide by 0";
throw io;
}
val = work.Num1 / work.Num2;
break;
default:
{
InvalidOperation io = new InvalidOperation();
io.What = (int)work.Op;
io.Why = "Unknown operation";
throw io;
}
}
return val;
}
public void zip()
{
Console.WriteLine("zip()");
}
}
}
2. 实现Main方法:
using System;
using Thrift.Server;
using Thrift.Transport;
namespace thrift_1stApp_server
{
class Program
{
public static void Main()
{
try
{
CalculatorHandler handler = new CalculatorHandler();
Calculator.Processor processor = new Calculator.Processor(handler);
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, serverTransport);
Console.WriteLine("Starting the server...");
server.Serve();
}
catch (Exception x)
{
Console.WriteLine(x.StackTrace);
}
Console.WriteLine("done.");
}
}
}
3. 编译运行,控制台会显示:Starting the Server...,然后就等待。
【开始编写Client端代码】:
.\tutorial-tutorial.thrift
4. 应该可以看到,多了一个gen-nodejs的目录
5. TBD...