如何实现C#Thrift服务并使用Silverlight客户端使用它?

时间:2012-01-30 14:32:00

标签: c# silverlight thrift

我目前正在将Thrift用作我们应用程序的RPC框架(主要用C#和Silverlight编写)。我已经实现了一个服务并从C#控制台应用程序(使用套接字作为传输)消费它。

对于C#服务器端代码,我的代码看起来像:(基本上复制了源代码中包含的教程)

MyServiceHandler handler = new MyServiceHandler();
MyService.Processor processor = new MyService.Processor(handler);
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, serverTransport);
server.Serve();

对于客户端代码,它看起来像:

TTransport transport = new TSocket("localhost", 9090);
TProtocol protocol = new TBinaryProtocol(transport);
MyService.Client client = new MyService.Client(protocol);
transport.Open();
client.SomeServiceCall();

但是,我们将从Silverlight客户端使用该服务,遗憾的是,Silverlight for Thrift中不支持套接字。我假设我被迫使用Thrift的C#THttpClient和THttpHandler类在客户端和服务之间使用HTTP通信?我找不到任何关于如何做到这一点的例子,有人能指出我正确的方向吗?一些示例服务器和客户端代码将不胜感激。

2 个答案:

答案 0 :(得分:1)

this guy似乎已经解决了这个问题。根据{{​​3}},修复程序在Thrift 0.9中可用。您可以尝试this JIRA(请注意,因为它不是最终版本,可能不稳定)或者您可以将this snapshot应用于0.8版本。

答案 1 :(得分:0)

我相信你现在已经理解,使用Thrift或任何其他客户端,没有直接的方式从Silverlight到Cassandra数据库进行通信。

我有一个与此相关的简单选项。编写支持Silverlight的Web服务并从客户端使用它。

例如,在服务器端,您可以拥有一个插入/更新/读取等的Web服务,就像这样。我只是设法提取了一些我们用于项目的代码。希望这会有所帮助。

using Apache.Cassandra;
using Thrift.Protocol;
using Thrift.Transport;

namespace CassandraWebLibrary
{
    public class MyDb
    {
        String _host;
        int _port;
        String _keyspace;
        bool _isConnected;
        TTransport _transport = null;
        Apache.Cassandra.Cassandra.Client _client = null;
        String columnFamily = "ColumnFamilyName";
        public VazhikaattiDB(String host, int port, String keyspace)
        {
            _host = host;
            _port = port;
            _keyspace = keyspace;
            _isConnected = false;
        }

        public bool Connect()
        {
            try
            {
                _transport = new TFramedTransport(new TSocket(_host, _port));
                TProtocol protocol = new TBinaryProtocol(_transport);
                _client = new Apache.Cassandra.Cassandra.Client(protocol);

                _transport.Open();

                _client.set_keyspace(_keyspace);

                _isConnected = true;
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            return _isConnected;
        }

        public bool Close()
        {
            if (_transport.IsOpen)
                _transport.Close();
            _isConnected = false;
            return true;
        }

        public bool InsertData(Send your data as parameters here)
        {
            try
            {
                List<Column> list = new List<Column>();
                string strKey = keyvalue;

                #region Inserting into Coulmn family
                 List<Byte> valbytes = new List<byte>(BitConverter.GetBytes(value)); //You might have to pad this with more bytes to make it length of 8 bytes

                Column doublecolumn1 = new Column()
                {
                    Name = Encoding.UTF8.GetBytes("column1"),
                    Timestamp = timestampvalue,
                    Value = valbytes.ToArray()
                };
                list.Add(doublecolumn1);

                Column stringcolumn2 = new Column()
                {
                    Name = Encoding.UTF8.GetBytes("column2"),
                    Timestamp = timestampvalue,
                    Value = Encoding.UTF8.GetBytes("StringValue")
                };
                list.Add(stringcolumn2);

                Column timecolumn3 = new Column()
                {
                    Name = Encoding.UTF8.GetBytes("column3"),
                    Timestamp = timestampvalue,
                    Value = BitConverter.GetBytes(DateTime.Now.Ticks)
                };
                list.Add(timecolumn3);
                #endregion


                ColumnParent columnParent = new ColumnParent();
                columnParent.Column_family = columnFamily;

                Byte[] key = Encoding.UTF8.GetBytes(strKey);
                foreach (Column column in list)
                {
                    try
                    {
                        _client.insert(key, columnParent, column, ConsistencyLevel.QUORUM);
                    }
                    catch (Exception e)
                    {
                        log.Error(e.ToString());
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                return false;
            }
        }

        public List<YourReturnObject> GetData(parameters)
        {
            try
            {
                ColumnParent columnParent = new ColumnParent();
                columnParent.Column_family = columnFamily;
                DateTime curdate = startdate;

                IndexExpression indExprsecondkey = new IndexExpression();
                indExprsecondkey.Column_name = Encoding.UTF8.GetBytes("column");
                indExprsecondkey.Op = IndexOperator.EQ;

                List<Byte> valbytes = PadLeftBytes((int)yourid, 8);
                indExprsecondkey.Value = valbytes.ToArray();
                indExprList.Add(indExprsecondkey);


                IndexClause indClause = new IndexClause()
                {
                    Expressions = indExprList,
                    Count = 1000,
                    Start_key = Encoding.UTF8.GetBytes("")
                };

                SlicePredicate slice = new SlicePredicate()
                {
                    Slice_range = new SliceRange()
                    {
                        //Start and Finish cannot be null 
                        Start = new byte[0],
                        Finish = new byte[0],
                        Count = 1000,
                        Reversed = false
                    }
                };
                List<KeySlice> keyslices = _client.get_indexed_slices(columnParent, indClause, slice, ConsistencyLevel.ONE);
                foreach (KeySlice ks in keyslices)
                {
                    String stringcolumnvalue = Encoding.UTF8.GetString(cl.Column.Value);
                    double doublevalue= (Double)BitConverter.ToDouble(cl.Column.Value); 
                    long timeticks = BitConverter.ToInt64(cl.Column.Value, 0);
                    DateTime dtcolumntime = new DateTime(timeticks);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }

            return yourdatalist;
        }


    }
}

现在,您的webservice可以使用上述类,而Silverlight将使用它。顺便说一句,你必须处理其他银光问题,比如要从服务器/网络服务等下载的数据大小, 仅供参考,我们的Cassandra客户服务在9160端口运行。