Thrift應用比較廣泛,這里不介紹Thrift的基本概念和使用。Thrift對.net支持的很好,但自從.net core誕生引來,我曾多次關注Thrift的官方網站,看看對.net core是否提供支持。今天在看Thrift在Github上的代碼鏡像時,發現已經有netcore的實現了。地址:https://github.com/apache/thrift/tree/master/lib/netcore.但是看到介紹的第一段文字,不免覺得遺憾“Thrift client library ported to Microsoft .Net Core”,難道只支持Client?於是搜了很多資料,並沒有太多介紹,甚至Client的Demo也沒有。於是嘗試翻看代碼,似乎有相關的Server實現,可能是還沒有正式發布的緣故。
不管怎么樣,動手試一下。
1、准備工作
下載Thrift的netcore部分的代碼,是一個.net core的項目,編譯通過。
下載Thrift compiler for Windows代碼生成工具,thrift-0.11.0.exe,可到官網下載。
編寫thrift IDL文件:
namespace netcore ThriftCore service Calculator{ void ping(), i32 add(1:i32 num1, 2:i32 num2) }
在命令行中執行“thrift-0.11.0.exe --gen netcore tutorial.thrift”,會在目錄下生成“gen-netcore\ThriftCore\Calculator.cs”。這部分使用與以前一致,只是語言部分需要指定netcore,而不是csharp。完成后,將Calculator.cs加入到項目中,開始編寫服務器和客戶端。
2、服務器端實現:
3、客戶端實現:
最后驗證結果:
4、結論
Thrift能夠實現.net core下的Server和Client。根據Github上的描述,.net core在部分功能上支持不全面,期待早日有穩定版的發布。