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

发布于 2024-12-31 21:32:45 字数 891 浏览 1 评论 0原文

我目前正在考虑将 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 中的套接字。我假设我被迫在客户端和服务之间使用 HTTP 通信,使用 Thrift 的 C# THttpClient 和 THttpHandler 类?我找不到任何如何做到这一点的例子,有人能指出我正确的方向吗?一些示例服务器和客户端代码将不胜感激。

I'm current looking at Thrift to use as a RPC framework for our apps (mostly written in C# and Silverlight). I've come as far as implementing a service and consuming it from a C# console app (using a socket as transport).

For the C# server side code my code looked like: (basically copying the tutorials included with the source code)

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

For the client side code it looked like:

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

However, we will be consuming the service from a Silverlight client, and unfortunately there is no support for sockets in Silverlight for Thrift. I assume I'm forced to use HTTP communication between the client and service, using Thrift's C# THttpClient and THttpHandler classes? I could not find any examples of how to do this out there, can anyone point me in the right direction? Some example server and client side code would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一笑 2025-01-07 21:32:45

看来这个问题已经被 this 解决了家伙。根据 此 JIRA,该修复已在 Thrift 0.9 中提供。您可以尝试此快照(请注意,由于它不是最终版本,因此可能不稳定)或者你可以应用 此补丁到 0.8 版本。

It seems that this issue was already addressed by this guy. According to this JIRA, the fix is available in Thrift 0.9. You can either try this snapshot (note that, as it's not a final release, it might not be stable) or you can apply this patch to the 0.8 release.

贵在坚持 2025-01-07 21:32:45

我相信现在您应该已经明白,无论是使用 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;
        }


    }
}

现在您的 Web 服务可以使用上面的类,而 Silverlight 又可以使用该 Web 服务。顺便说一句,您必须处理其他 silverlight 问题,例如从服务器/网络服务下载的数据大小等,
仅供参考,我们的 Cassandra 客户端服务运行在端口 9160 上。

I believe by now you would have understood, there is no direct way of communicating from Silverlight to the Cassandra database either using Thrift or any other clients.

I have one simple option related to this. Write a Silverlight enabled web service and consume it from the client.

For example, on the server side you can have a web service which does insert/update/read etc., like this. I just managed to pull out some code which we use for our project. Hope this helps.

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;
        }


    }
}

Now the above class can be used by your webservice, which in turn will be used by Silverlight. Btw, you'll have to take care of other silverlight issues like size of data to be downloaded from server/webservice etc.,
FYI, our client service of Cassandra runs on port 9160..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文