部分实现参考博主其他文章:
http://blog.youkuaiyun.com/ceasadan/article/details/52277136 《安装Thrift并写一个简单的测试程序》
http://blog.youkuaiyun.com/ceasadan/article/details/52317586 《Thrift小试牛刀:实现Windows_C#_客户端与Linux_C++_服务端通信》
1. 先从官网下载Thrift源码及Thrift.exe
http://thrift.apache.org/download
2.设置自己的环境变量
a.将下载的exe文件改名改成thrift.exe 放在c:\windows目录下
b.设置环境变量:找到Path,在最后添加目录:c:\windows
c.打开cmd 输入thrift可看到配置成功
3.编译源码,生成Thrift.dll
参考:http://blog.youkuaiyun.com/ceasadan/article/details/52317586 《Thrift小试牛刀:实现Windows_C#_客户端与Linux_C++_服务端通信》
4.编写demo.thrift文件
参考:http://blog.youkuaiyun.com/ceasadan/article/details/52317586 《Thrift小试牛刀:实现Windows_C#_客户端与Linux_C++_服务端通信》
5.根据demo.thrift生成C#文件
thrift -r --gen csharp demo.thrift
生成文件:UserProfile.cs和UserStorage.cs
6.新建C#控制台程序
将UserProfile.cs和UserStorage.cs加入工程;
引用Thrift.dll;
添加接口实现类:HelloWroldImpl.cs
class HelloWroldImpl : UserStorage.Iface
{
private Dictionary<Int32, UserProfile> log = new Dictionary<int,UserProfile>();
public void store(UserProfile user)
{
// Your implementation goes here
log[user.Id] = user;
Console.WriteLine("store\n");
}
public UserProfile getUser(int uid)
{
// Your implementation goes here
Console.WriteLine("getUser\n");
return log[uid];
}
}
main函数添加:
<span style="white-space:pre"> </span> try
{
HelloWroldImpl handler = new HelloWroldImpl();
UserStorage.Processor processor = new UserStorage.Processor(handler);
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, serverTransport);
// Use this for a multithreaded server
// server = new TThreadPoolServer(processor, serverTransport);
Console.WriteLine("Starting the server...");
server.Serve();
}
catch (Exception x)
{
Console.WriteLine(x.StackTrace);
}
Console.WriteLine("done.");
7.客户端实现参考:(与下面链接一样)
http://blog.youkuaiyun.com/ceasadan/article/details/52317586 《Thrift小试牛刀:实现Windows_C#_客户端与Linux_C++_服务端通信》