1. 程式人生 > >Thrift是一款由Fackbook開發的可伸縮、跨語言的服務開發框架

Thrift是一款由Fackbook開發的可伸縮、跨語言的服務開發框架

final 問題 不同 err gin 之間 統一 tex cgi

這段時間,一直在整理公司的內部 rpc 服務接口,面臨的一個問題就是:由於公司內部的系統由幾個不同的語言編寫的。C# ,java,node.js 等,如何實現這些內部系統之間的接口統一調用,確實是比較麻煩,本來考慮用webapi 但是感覺內部系統之間用webapi 效率不高。最終,我們還是考慮引入Thrift ,通過Thrift整合各個不同的RPC服務。下面就Thrift 如何使用,做個簡單的介紹,本人也是初次接觸。

  介紹

  Thrift是一款由Fackbook開發的可伸縮、跨語言的服務開發框架,該框架已經開源並且加入的Apache項目。Thrift主要功能是:通過自定義的Interface Definition Language(IDL),可以創建基於RPC的客戶端和服務端的服務代碼。數據和服務代碼的生成是通過Thrift內置的代碼生成器來實現的。Thrift 的跨語言性體現在,它可以生成C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml , Delphi等語言的代碼,且它們之間可以進行透明的通信。

  Thrift 使得各個不同的語言的系統之間可以進行透明高效的通信。但是,Fackbook 的一貫作風就是只管發布,不管維護。所以,Thrift 目前還存在一些為解決的問題。大家在調研的時候,應該考慮清楚。

  本文結合網絡上的資源對從C#開發人員的角度簡單介紹Thrift的使用,並且針對不同的傳輸協議和服務類型給出相應的C#實例,同時簡單介紹Thrift異步客戶端的實現。

  Thrift代碼生成器windows版下載地址

  http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.exe

  Thrift源碼下載地址

  http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.tar.gz

  Window 下安裝配置

  Thrift 不需要安裝,只需下載windows版的 Thrift代碼生成器即可,下載地址如上連接

  技術分享

  註意:下載下來之後,必須把文件名字thrift-0.10.0.exe 改為 thrift.exe, 否則cmd會提示:thrift 不是內部命令的錯誤。

  創建thrift的語法規範編寫腳本文件

  根據thrift的語法規範編寫腳本文件Hello.thrift,代碼如下:

技術分享
namespace csharp HelloThrift.Interface

service HelloService{

    string HelloString(1:string para)

    i32 HelloInt(1:i32 para)

    bool HelloBoolean(1:bool para)

    void HelloVoid()

    string HelloNull()

}
技術分享

  生成Csharp 版的服務定義類

  然後打開cmd切換到thrift代碼生成工具的存放目錄,在命令行中輸入如下命令:thrift -gen csharp Hello.thrift

  技術分享

  代碼生成工具會自動在當前目錄下把定義好的接口腳本生成C#代碼,生成後的代碼目錄如下

  技術分享

  接口腳本生成C#代碼

技術分享
/**
 * Autogenerated by Thrift Compiler (0.9.3)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Thrift;
using Thrift.Collections;
using System.Runtime.Serialization;
using Thrift.Protocol;
using Thrift.Transport;

namespace HelloThrift.Interface
{
  public partial class HelloService {
    public interface Iface {
      string HelloString(string para);
      #if SILVERLIGHT
      IAsyncResult Begin_HelloString(AsyncCallback callback, object state, string para);
      string End_HelloString(IAsyncResult asyncResult);
      #endif
      int HelloInt(int para);
      #if SILVERLIGHT
      IAsyncResult Begin_HelloInt(AsyncCallback callback, object state, int para);
      int End_HelloInt(IAsyncResult asyncResult);
      #endif
      bool HelloBoolean(bool para);
      #if SILVERLIGHT
      IAsyncResult Begin_HelloBoolean(AsyncCallback callback, object state, bool para);
      bool End_HelloBoolean(IAsyncResult asyncResult);
      #endif
      void HelloVoid();
      #if SILVERLIGHT
      IAsyncResult Begin_HelloVoid(AsyncCallback callback, object state);
      void End_HelloVoid(IAsyncResult asyncResult);
      #endif
      string HelloNull();
      #if SILVERLIGHT
      IAsyncResult Begin_HelloNull(AsyncCallback callback, object state);
      string End_HelloNull(IAsyncResult asyncResult);
      #endif
    }

    public class Client : IDisposable, Iface {
      public Client(TProtocol prot) : this(prot, prot)
      {
      }

      public Client(TProtocol iprot, TProtocol oprot)
      {
        iprot_ = iprot;
        oprot_ = oprot;
      }

      protected TProtocol iprot_;
      protected TProtocol oprot_;
      protected int seqid_;

      public TProtocol InputProtocol
      {
        get { return iprot_; }
      }
      public TProtocol OutputProtocol
      {
        get { return oprot_; }
      }


      #region " IDisposable Support "
      private bool _IsDisposed;

      // IDisposable
      public void Dispose()
      {
        Dispose(true);
      }
      

      protected virtual void Dispose(bool disposing)
      {
        if (!_IsDisposed)
        {
          if (disposing)
          {
            if (iprot_ != null)
            {
              ((IDisposable)iprot_).Dispose();
            }
            if (oprot_ != null)
            {
              ((IDisposable)oprot_).Dispose();
            }
          }
        }
        _IsDisposed = true;
      }
      #endregion


      
      #if SILVERLIGHT
      public IAsyncResult Begin_HelloString(AsyncCallback callback, object state, string para)
      {
        return send_HelloString(callback, state, para);
      }

      public string End_HelloString(IAsyncResult asyncResult)
      {
        oprot_.Transport.EndFlush(asyncResult);
        return recv_HelloString();
      }

      #endif

      public string HelloString(string para)
      {
        #if !SILVERLIGHT
        send_HelloString(para);
        return recv_HelloString();

        #else
        var asyncResult = Begin_HelloString(null, null, para);
        return End_HelloString(asyncResult);

        #endif
      }
      #if SILVERLIGHT
      public IAsyncResult send_HelloString(AsyncCallback callback, object state, string para)
      #else
      public void send_HelloString(string para)
      #endif
      {
        oprot_.WriteMessageBegin(new TMessage("HelloString", TMessageType.Call, seqid_));
        HelloString_args args = new HelloString_args();
        args.Para = para;
        args.Write(oprot_);
        oprot_.WriteMessageEnd();
        #if SILVERLIGHT
        return oprot_.Transport.BeginFlush(callback, state);
        #else
        oprot_.Transport.Flush();
        #endif
      }

      public string recv_HelloString()
      {
        TMessage msg = iprot_.ReadMessageBegin();
        if (msg.Type == TMessageType.Exception) {
          TApplicationException x = TApplicationException.Read(iprot_);
          iprot_.ReadMessageEnd();
          throw x;
        }
        HelloString_result result = new HelloString_result();
        result.Read(iprot_);
        iprot_.ReadMessageEnd();
        if (result.__isset.success) {
          return result.Success;
        }
        throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "HelloString failed: unknown result");
      }

      
      #if SILVERLIGHT
      public IAsyncResult Begin_HelloInt(AsyncCallback callback, object state, int para)
      {
        return send_HelloInt(callback, state, para);
      }

      public int End_HelloInt(IAsyncResult asyncResult)
      {
        oprot_.Transport.EndFlush(asyncResult);
        return recv_HelloInt();
      }

      #endif

      public int HelloInt(int para)
      {
        #if !SILVERLIGHT
        send_HelloInt(para);
        return recv_HelloInt();

        #else
        var asyncResult = Begin_HelloInt(null, null, para);
        return End_HelloInt(asyncResult);

        #endif
      }
      #if SILVERLIGHT
      public IAsyncResult send_HelloInt(AsyncCallback callback, object state, int para)
      #else
      public void send_HelloInt(int para)
      #endif
      {
        oprot_.WriteMessageBegin(new TMessage("HelloInt", TMessageType.Call, seqid_));
        HelloInt_args args = new HelloInt_args();
        args.Para = para;
        args.Write(oprot_);
        oprot_.WriteMessageEnd();
        #if SILVERLIGHT
        return oprot_.Transport.BeginFlush(callback, state);
        #else
        oprot_.Transport.Flush();
        #endif
      }

      public int recv_HelloInt()
      {
        TMessage msg = iprot_.ReadMessageBegin();
        if (msg.Type == TMessageType.Exception) {
          TApplicationException x = TApplicationException.Read(iprot_);
          iprot_.ReadMessageEnd();
          throw x;
        }
        HelloInt_result result = new HelloInt_result();
        result.Read(iprot_);
        iprot_.ReadMessageEnd();
        if (result.__isset.success) {
          return result.Success;
        }
        throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "HelloInt failed: unknown result");
      }

      
      #if SILVERLIGHT
      public IAsyncResult Begin_HelloBoolean(AsyncCallback callback, object state, bool para)
      {
        return send_HelloBoolean(callback, state, para);
      }

      public bool End_HelloBoolean(IAsyncResult asyncResult)
      {
        oprot_.Transport.EndFlush(asyncResult);
        return recv_HelloBoolean();
      }

      #endif

      public bool HelloBoolean(bool para)
      {
        #if !SILVERLIGHT
        send_HelloBoolean(para);
        return recv_HelloBoolean();

        #else
        var asyncResult = Begin_HelloBoolean(null, null, para);
        return End_HelloBoolean(asyncResult);

        #endif
      }
      #if SILVERLIGHT
      public IAsyncResult send_HelloBoolean(AsyncCallback callback, object state, bool para)
      #else
      public void send_HelloBoolean(bool para)
      #endif
      {
        oprot_.WriteMessageBegin(new TMessage("HelloBoolean", TMessageType.Call, seqid_));
        HelloBoolean_args args = new HelloBoolean_args();
        args.Para = para;
        args.Write(oprot_);
        oprot_.WriteMessageEnd();
        #if SILVERLIGHT
        return oprot_.Transport.BeginFlush(callback, state);
        #else
        oprot_.Transport.Flush();
        #endif
      }

      public bool recv_HelloBoolean()
      {
        TMessage msg = iprot_.ReadMessageBegin();
        if (msg.Type == TMessageType.Exception) {
          TApplicationException x = TApplicationException.Read(iprot_);
          iprot_.ReadMessageEnd();
          throw x;
        }
        HelloBoolean_result result = new HelloBoolean_result();
        result.Read(iprot_);
        iprot_.ReadMessageEnd();
        if (result.__isset.success) {
          return result.Success;
        }
        throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "HelloBoolean failed: unknown result");
      }

      
      #if SILVERLIGHT
      public IAsyncResult Begin_HelloVoid(AsyncCallback callback, object state)
      {
        return send_HelloVoid(callback, state);
      }

      public void End_HelloVoid(IAsyncResult asyncResult)
      {
        oprot_.Transport.EndFlush(asyncResult);
        recv_HelloVoid();
      }

      #endif

      public void HelloVoid()
      {
        #if !SILVERLIGHT
        send_HelloVoid();
        recv_HelloVoid();

        #else
        var asyncResult = Begin_HelloVoid(null, null);
        End_HelloVoid(asyncResult);

        #endif
      }
      #if SILVERLIGHT
      public IAsyncResult send_HelloVoid(AsyncCallback callback, object state)
      #else
      public void send_HelloVoid()
      #endif
      {
        oprot_.WriteMessageBegin(new TMessage("HelloVoid", TMessageType.Call, seqid_));
        HelloVoid_args args = new HelloVoid_args();
        args.Write(oprot_);
        oprot_.WriteMessageEnd();
        #if SILVERLIGHT
        return oprot_.Transport.BeginFlush(callback, state);
        #else
        oprot_.Transport.Flush();
        #endif
      }

      public void recv_HelloVoid()
      {
        TMessage msg = iprot_.ReadMessageBegin();
        if (msg.Type == TMessageType.Exception) {
          TApplicationException x = TApplicationException.Read(iprot_);
          iprot_.ReadMessageEnd();
          throw x;
        }
        HelloVoid_result result = new HelloVoid_result();
        result.Read(iprot_);
        iprot_.ReadMessageEnd();
        return;
      }

      
      #if SILVERLIGHT
      public IAsyncResult Begin_HelloNull(AsyncCallback callback, object state)
      {
        return send_HelloNull(callback, state);
      }

      public string End_HelloNull(IAsyncResult asyncResult)
      {
        oprot_.Transport.EndFlush(asyncResult);
        return recv_HelloNull();
      }

      #endif

      public string HelloNull()
      {
        #if !SILVERLIGHT
        send_HelloNull();
        return recv_HelloNull();

        #else
        var asyncResult = Begin_HelloNull(null, null);
        return End_HelloNull(asyncResult);

        #endif
      }
      #if SILVERLIGHT
      public IAsyncResult send_HelloNull(AsyncCallback callback, object state)
      #else
      public void send_HelloNull()
      #endif
      {
        oprot_.WriteMessageBegin(new TMessage("HelloNull", TMessageType.Call, seqid_));
        HelloNull_args args = new HelloNull_args();
        args.Write(oprot_);
        oprot_.WriteMessageEnd();
        #if SILVERLIGHT
        return oprot_.Transport.BeginFlush(callback, state);
        #else
        oprot_.Transport.Flush();
        #endif
      }

      public string recv_HelloNull()
      {
        TMessage msg = iprot_.ReadMessageBegin();
        if (msg.Type == TMessageType.Exception) {
          TApplicationException x = TApplicationException.Read(iprot_);
          iprot_.ReadMessageEnd();
          throw x;
        }
        HelloNull_result result = new HelloNull_result();
        result.Read(iprot_);
        iprot_.ReadMessageEnd();
        if (result.__isset.success) {
          return result.Success;
        }
        throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "HelloNull failed: unknown result");
      }

    }
    public class Processor : TProcessor {
      public Processor(Iface iface)
      {
        iface_ = iface;
        processMap_["HelloString"] = HelloString_Process;
        processMap_["HelloInt"] = HelloInt_Process;
        processMap_["HelloBoolean"] = HelloBoolean_Process;
        processMap_["HelloVoid"] = HelloVoid_Process;
        processMap_["HelloNull"] = HelloNull_Process;
      }

      protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot);
      private Iface iface_;
      protected Dictionary<string, ProcessFunction> processMap_ = new Dictionary<string, ProcessFunction>();

      public bool Process(TProtocol iprot, TProtocol oprot)
      {
        try
        {
          TMessage msg = iprot.ReadMessageBegin();
          ProcessFunction fn;
          processMap_.TryGetValue(msg.Name, out fn);
          if (fn == null) {
            TProtocolUtil.Skip(iprot, TType.Struct);
            iprot.ReadMessageEnd();
            TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.UnknownMethod, "Invalid method name: ‘" + msg.Name + "‘");
            oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));
            x.Write(oprot);
            oprot.WriteMessageEnd();
            oprot.Transport.Flush();
            return true;
          }
          fn(msg.SeqID, iprot, oprot);
        }
        catch (IOException)
        {
          return false;
        }
        return true;
      }

      public void HelloString_Process(int seqid, TProtocol iprot, TProtocol oprot)
      {
        HelloString_args args = new HelloString_args();
        args.Read(iprot);
        iprot.ReadMessageEnd();
        HelloString_result result = new HelloString_result();
        result.Success = iface_.HelloString(args.Para);
        oprot.WriteMessageBegin(new TMessage("HelloString", TMessageType.Reply, seqid)); 
        result.Write(oprot);
        oprot.WriteMessageEnd();
        oprot.Transport.Flush();
      }

      public void HelloInt_Process(int seqid, TProtocol iprot, TProtocol oprot)
      {
        HelloInt_args args = new HelloInt_args();
        args.Read(iprot);
        iprot.ReadMessageEnd();
        HelloInt_result result = new HelloInt_result();
        result.Success = iface_.HelloInt(args.Para);
        oprot.WriteMessageBegin(new TMessage("HelloInt", TMessageType.Reply, seqid)); 
        result.Write(oprot);
        oprot.WriteMessageEnd();
        oprot.Transport.Flush();
      }

      public void HelloBoolean_Process(int seqid, TProtocol iprot, TProtocol oprot)
      {
        HelloBoolean_args args = new HelloBoolean_args();
        args.Read(iprot);
        iprot.ReadMessageEnd();
        HelloBoolean_result result = new HelloBoolean_result();
        result.Success = iface_.HelloBoolean(args.Para);
        oprot.WriteMessageBegin(new TMessage("HelloBoolean", TMessageType.Reply, seqid)); 
        result.Write(oprot);
        oprot.WriteMessageEnd();
        oprot.Transport.Flush();
      }

      public void HelloVoid_Process(int seqid, TProtocol iprot, TProtocol oprot)
      {
        HelloVoid_args args = new HelloVoid_args();
        args.Read(iprot);
        iprot.ReadMessageEnd();
        HelloVoid_result result = new HelloVoid_result();
        iface_.HelloVoid();
        oprot.WriteMessageBegin(new TMessage("HelloVoid", TMessageType.Reply, seqid)); 
        result.Write(oprot);
        oprot.WriteMessageEnd();
        oprot.Transport.Flush();
      }

      public void HelloNull_Process(int seqid, TProtocol iprot, TProtocol oprot)
      {
        HelloNull_args args = new HelloNull_args();
        args.Read(iprot);
        iprot.ReadMessageEnd();
        HelloNull_result result = new HelloNull_result();
        result.Success = iface_.HelloNull();
        oprot.WriteMessageBegin(new TMessage("HelloNull", TMessageType.Reply, seqid)); 
        result.Write(oprot);
        oprot.WriteMessageEnd();
        oprot.Transport.Flush();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloString_args : TBase
    {
      private string _para;

      public string Para
      {
        get
        {
          return _para;
        }
        set
        {
          __isset.para = true;
          this._para = value;
        }
      }


      public Isset __isset;
      #if !SILVERLIGHT
      [Serializable]
      #endif
      public struct Isset {
        public bool para;
      }

      public HelloString_args() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              case 1:
                if (field.Type == TType.String) {
                  Para = iprot.ReadString();
                } else { 
                  TProtocolUtil.Skip(iprot, field.Type);
                }
                break;
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloString_args");
          oprot.WriteStructBegin(struc);
          TField field = new TField();
          if (Para != null && __isset.para) {
            field.Name = "para";
            field.Type = TType.String;
            field.ID = 1;
            oprot.WriteFieldBegin(field);
            oprot.WriteString(Para);
            oprot.WriteFieldEnd();
          }
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloString_args(");
        bool __first = true;
        if (Para != null && __isset.para) {
          if(!__first) { __sb.Append(", "); }
          __first = false;
          __sb.Append("Para: ");
          __sb.Append(Para);
        }
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloString_result : TBase
    {
      private string _success;

      public string Success
      {
        get
        {
          return _success;
        }
        set
        {
          __isset.success = true;
          this._success = value;
        }
      }


      public Isset __isset;
      #if !SILVERLIGHT
      [Serializable]
      #endif
      public struct Isset {
        public bool success;
      }

      public HelloString_result() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              case 0:
                if (field.Type == TType.String) {
                  Success = iprot.ReadString();
                } else { 
                  TProtocolUtil.Skip(iprot, field.Type);
                }
                break;
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloString_result");
          oprot.WriteStructBegin(struc);
          TField field = new TField();

          if (this.__isset.success) {
            if (Success != null) {
              field.Name = "Success";
              field.Type = TType.String;
              field.ID = 0;
              oprot.WriteFieldBegin(field);
              oprot.WriteString(Success);
              oprot.WriteFieldEnd();
            }
          }
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloString_result(");
        bool __first = true;
        if (Success != null && __isset.success) {
          if(!__first) { __sb.Append(", "); }
          __first = false;
          __sb.Append("Success: ");
          __sb.Append(Success);
        }
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloInt_args : TBase
    {
      private int _para;

      public int Para
      {
        get
        {
          return _para;
        }
        set
        {
          __isset.para = true;
          this._para = value;
        }
      }


      public Isset __isset;
      #if !SILVERLIGHT
      [Serializable]
      #endif
      public struct Isset {
        public bool para;
      }

      public HelloInt_args() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              case 1:
                if (field.Type == TType.I32) {
                  Para = iprot.ReadI32();
                } else { 
                  TProtocolUtil.Skip(iprot, field.Type);
                }
                break;
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloInt_args");
          oprot.WriteStructBegin(struc);
          TField field = new TField();
          if (__isset.para) {
            field.Name = "para";
            field.Type = TType.I32;
            field.ID = 1;
            oprot.WriteFieldBegin(field);
            oprot.WriteI32(Para);
            oprot.WriteFieldEnd();
          }
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloInt_args(");
        bool __first = true;
        if (__isset.para) {
          if(!__first) { __sb.Append(", "); }
          __first = false;
          __sb.Append("Para: ");
          __sb.Append(Para);
        }
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloInt_result : TBase
    {
      private int _success;

      public int Success
      {
        get
        {
          return _success;
        }
        set
        {
          __isset.success = true;
          this._success = value;
        }
      }


      public Isset __isset;
      #if !SILVERLIGHT
      [Serializable]
      #endif
      public struct Isset {
        public bool success;
      }

      public HelloInt_result() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              case 0:
                if (field.Type == TType.I32) {
                  Success = iprot.ReadI32();
                } else { 
                  TProtocolUtil.Skip(iprot, field.Type);
                }
                break;
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloInt_result");
          oprot.WriteStructBegin(struc);
          TField field = new TField();

          if (this.__isset.success) {
            field.Name = "Success";
            field.Type = TType.I32;
            field.ID = 0;
            oprot.WriteFieldBegin(field);
            oprot.WriteI32(Success);
            oprot.WriteFieldEnd();
          }
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloInt_result(");
        bool __first = true;
        if (__isset.success) {
          if(!__first) { __sb.Append(", "); }
          __first = false;
          __sb.Append("Success: ");
          __sb.Append(Success);
        }
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloBoolean_args : TBase
    {
      private bool _para;

      public bool Para
      {
        get
        {
          return _para;
        }
        set
        {
          __isset.para = true;
          this._para = value;
        }
      }


      public Isset __isset;
      #if !SILVERLIGHT
      [Serializable]
      #endif
      public struct Isset {
        public bool para;
      }

      public HelloBoolean_args() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              case 1:
                if (field.Type == TType.Bool) {
                  Para = iprot.ReadBool();
                } else { 
                  TProtocolUtil.Skip(iprot, field.Type);
                }
                break;
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloBoolean_args");
          oprot.WriteStructBegin(struc);
          TField field = new TField();
          if (__isset.para) {
            field.Name = "para";
            field.Type = TType.Bool;
            field.ID = 1;
            oprot.WriteFieldBegin(field);
            oprot.WriteBool(Para);
            oprot.WriteFieldEnd();
          }
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloBoolean_args(");
        bool __first = true;
        if (__isset.para) {
          if(!__first) { __sb.Append(", "); }
          __first = false;
          __sb.Append("Para: ");
          __sb.Append(Para);
        }
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloBoolean_result : TBase
    {
      private bool _success;

      public bool Success
      {
        get
        {
          return _success;
        }
        set
        {
          __isset.success = true;
          this._success = value;
        }
      }


      public Isset __isset;
      #if !SILVERLIGHT
      [Serializable]
      #endif
      public struct Isset {
        public bool success;
      }

      public HelloBoolean_result() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              case 0:
                if (field.Type == TType.Bool) {
                  Success = iprot.ReadBool();
                } else { 
                  TProtocolUtil.Skip(iprot, field.Type);
                }
                break;
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloBoolean_result");
          oprot.WriteStructBegin(struc);
          TField field = new TField();

          if (this.__isset.success) {
            field.Name = "Success";
            field.Type = TType.Bool;
            field.ID = 0;
            oprot.WriteFieldBegin(field);
            oprot.WriteBool(Success);
            oprot.WriteFieldEnd();
          }
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloBoolean_result(");
        bool __first = true;
        if (__isset.success) {
          if(!__first) { __sb.Append(", "); }
          __first = false;
          __sb.Append("Success: ");
          __sb.Append(Success);
        }
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloVoid_args : TBase
    {

      public HelloVoid_args() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloVoid_args");
          oprot.WriteStructBegin(struc);
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloVoid_args(");
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloVoid_result : TBase
    {

      public HelloVoid_result() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloVoid_result");
          oprot.WriteStructBegin(struc);

          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloVoid_result(");
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloNull_args : TBase
    {

      public HelloNull_args() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloNull_args");
          oprot.WriteStructBegin(struc);
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloNull_args(");
        __sb.Append(")");
        return __sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloNull_result : TBase
    {
      private string _success;

      public string Success
      {
        get
        {
          return _success;
        }
        set
        {
          __isset.success = true;
          this._success = value;
        }
      }


      public Isset __isset;
      #if !SILVERLIGHT
      [Serializable]
      #endif
      public struct Isset {
        public bool success;
      }

      public HelloNull_result() {
      }

      public void Read (TProtocol iprot)
      {
        iprot.IncrementRecursionDepth();
        try
        {
          TField field;
          iprot.ReadStructBegin();
          while (true)
          {
            field = iprot.ReadFieldBegin();
            if (field.Type == TType.Stop) { 
              break;
            }
            switch (field.ID)
            {
              case 0:
                if (field.Type == TType.String) {
                  Success = iprot.ReadString();
                } else { 
                  TProtocolUtil.Skip(iprot, field.Type);
                }
                break;
              default: 
                TProtocolUtil.Skip(iprot, field.Type);
                break;
            }
            iprot.ReadFieldEnd();
          }
          iprot.ReadStructEnd();
        }
        finally
        {
          iprot.DecrementRecursionDepth();
        }
      }

      public void Write(TProtocol oprot) {
        oprot.IncrementRecursionDepth();
        try
        {
          TStruct struc = new TStruct("HelloNull_result");
          oprot.WriteStructBegin(struc);
          TField field = new TField();

          if (this.__isset.success) {
            if (Success != null) {
              field.Name = "Success";
              field.Type = TType.String;
              field.ID = 0;
              oprot.WriteFieldBegin(field);
              oprot.WriteString(Success);
              oprot.WriteFieldEnd();
            }
          }
          oprot.WriteFieldStop();
          oprot.WriteStructEnd();
        }
        finally
        {
          oprot.DecrementRecursionDepth();
        }
      }

      public override string ToString() {
        StringBuilder __sb = new StringBuilder("HelloNull_result(");
        bool __first = true;
        if (Success != null && __isset.success) {
          if(!__first) { __sb.Append(", "); }
          __first = false;
          __sb.Append("Success: ");
          __sb.Append(Success);
        }
        __sb.Append(")");
        return __sb.ToString();
      }

    }

  }
}
技術分享

  HelloService定義了服務HelloService的五個方法,每個方法包含一個方法名,參數列表和返回類型。每個參數包括參數序號,參數類型以及參數名。包含了在 Hello.thrift 文件中描述的服務 HelloService 的接口定義,即HelloService.Iface 接口,以及服務調用的底層通信細節,包括客戶端的調用邏輯HelloService.Client 以及服務器端的處理邏輯HelloService.Processor,用於構建客戶端和服務器端的功能。

  這樣,C#版的接口代碼就生成成功了。非常簡單,下一篇,會具體的介紹服務器端如何實現調用這些接口和客戶端如何調用這些接口。

Thrift是一款由Fackbook開發的可伸縮、跨語言的服務開發框架