Thrift应用:Windows下C#服务端实现


部分实现参考博主其他文章:

http://blog.csdn.net/ceasadan/article/details/52277136 《安装Thrift并写一个简单的测试程序》

http://blog.csdn.net/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.csdn.net/ceasadan/article/details/52317586  《Thrift小试牛刀:实现Windows_C#_客户端与Linux_C++_服务端通信》


4.编写demo.thrift文件

参考:http://blog.csdn.net/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.csdn.net/ceasadan/article/details/52317586  《Thrift小试牛刀:实现Windows_C#_客户端与Linux_C++_服务端通信》



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值