Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, August 3, 2014

Cannot read remote side has closed Thrift Cassandra C#

I was seeing this error ("Cannot read remote side has closed") while trying to get Thrift API to work with Cassandra using Csharp. For the life of me I couldn't figure out what was wrong as even though the error is saying something like "Hey I think your Thrift server is down". That was not the case because I was connecting to My Cassandra server using other client libraries that was using Thrift under the hood. Eventually I figured out the error, the offending line was transport.Open(); When I replaced it with cassandraClient.InputProtocol.Transport.Open(); The error went away.

Here is the full snippet,

            TSocket socket = null;

            TTransport transport = null;

        

            socket = new TSocket("localhost", 9160);

          

          

            transport = new TFramedTransport(socket);

            TProtocol protocol = new TBinaryProtocol(transport);

            CassandraClient cassandraClient = new CassandraClient(protocol);

            cassandraClient.InputProtocol.Transport.Open();



            string s = cassandraClient.describe_cluster_name();

            List<ksdef> keyspaces = cassandraClient.describe_keyspaces();





Hope this will help another poor soul.

Sunday, December 29, 2013

Func vs Action vs Predicate in C#

Often users find certain aspects of functional programming in C# confusing and this is one of them.

To simply put Action<T> and Predicate<T> are special cases of Func<T,TResult>. And all of these are delegate types provided by C# so that you do not need to define your own delegate type.

Action<T>
When you need a delegate that takes in a parameter (say an int) and returns nothing (void) then you have,

Action<int> which is equivalent to Func<int,void>

(Note that Func<int,void> is not something valid and wont work, this is just to draw a parallel between Action and Func. )

Predicate<T>
When you need a delegate that takes in a parameter (say an int ) and returns a bool then you have,

Predicate<int> which is equivalent to Func<int,bool>

Typically Linq uses Func and List<T> uses Action and Predicate.