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.

Thursday, June 26, 2014

SQL GROUP BY Example

The GROUP BY clause causes data in a table (or any data source) to be divided into groups based on the expressions (or conditions) given in the GROUP BY clause. Seems confusing? Probably.. Let's see what it means,

Say you have Column1, Column 2..etc on your table.

Now you say GROUP BY Column1 , what this does is it divides your table's data based on Column1. So all data which has  Column1='Apple' will be in one group, then all data which has  Column1='Orange' will be in another group.

Again if you had GROUP BY Column1, Column2 then your data will be divided into groups of Column1 + Column2 combinations . If Column2 had values of Green and Red in different rows then one group would be based on Apple+Red another group Apple+Green. So you get the point.

So in a nutshell,

GROUP BY creates groups for output rows, according to unique combination of values specified in the GROUP BY clause

But why would you need to divide them into groups? So we can operate on those groups.

I'll use AdventureWorks database as an example.

Download it here,
https://msftdbprodsamples.codeplex.com/releases/view/93587


SELECT SalesPersonID, YEAR(OrderDate) AS OrderYear,
COUNT(CustomerID) AS All_Customers
FROM Sales.SalesOrderHeader
GROUP BY SalesPersonID, YEAR(OrderDate);

The above query will return all unique combinations of SalesPersonID  + YEAR(OrderDate) and display all customers each of those combinations.

In other words this means you are seeing all customers for a particular Sales Person in a given year.

Point to note:

SELECT DISTINCT SalesPersonID ,YEAR(OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader

and

SELECT  SalesPersonID ,YEAR(OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader
GROUP BY SalesPersonID, YEAR(OrderDate);

Will give you the same results.

Saturday, May 24, 2014

ASP.Net Webforms Inline tags and code blocks

There are many elements inside a webforms (.aspx) page. To somebody beginning asp.net webforms these may seem quite similar and be confusing at first. (Well it was for me when I started learning asp.net webforms)
So hopefully this might help another soul.

Elements of a asp.net page (Inline tags and code blocks) can be listed as follows,

1. Directives

These come in many flavors like Page directives,
 ex : <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Trace="true" %>

or  Import directives,
ex : <%@ namespace="System.Drawing" %>

There are many more directives other than these two.

2. ASP.Net expressions

This is expression builder syntax, typically used to get values from web.config
ex: <%$ ConnectionStrings:AdventureWorks2008 %>  

3. User Controls

These are references to user created controls which are similar to web controls that enhances reusability 
ex: <PH:MainMenu Title="Menu Control" id="mainmenu" runat="server" />

4. Render blocks

Used for outputting elements like variables
ex:  If you have a variable named age then,
<%= age %>
will output the value of age

5. Code blocks

These are similar to javascript tags just that these run on the server,
<script runat="Server" language="c#" > ....some server side code here .... </script>  

6. Server Controls

These are the controls we drag and drop from the designer (Labels, Buttons etc)
ex: <asp:Label id="myLabel" runat="Server" />

7. Data binding controls

Used for biniding expressions such as Eval or Bind, these are typically used with data controls like Gridview
ex : <%# Eval("DBFieldName") %>

   

Tuesday, March 25, 2014

DataContract vs Serializable WCF

When applied to a class Serializable attribute serializes everything inside of it including private fields thus the developer has no control as what to expose.

When DataContract attribute is applied to a class it will serialize only the elements decorated with DataMember attribute thus the developer will have more control.

ServiceContract Name property WCF

This property can give your contract a name which is different from the actual interface name. So this way if you decide to change the interface name of a already running WCF service your client wont break.

Ex:

 [ServiceContract(Name="IGoodbyeService")]

    public interface IHelloService

    {

        [OperationContract]

        string GetMessage(string name);

    }


Now the contract name would be IGoodbyeService rather than  IHelloService.

To verify in the WSDL generated check the porttype property it should be IGoodbyeService

Note: Same can be done for OperationContract as well 

Monday, March 24, 2014

Basic Elements of WCF Appconfig

All config items should be inside ,

<system.serviceModel>

This is the WCF namespace provided by Microsoft.

Inside the <system.serviceModel> element you have the <services> element. This is where you configure all your services.
For each service you have a <service> element inside <services> element.
A particular service can be exposed using more than one endpoint. (Ex: http , net.tcp etc)
So for each endpoint in a service you configure a <endpoint> element.
An endpoint element will have three essential things,

  1. address
  2. binding
  3. contract  
Here is a  sample config (not complete) to get an idea.

The service name is HelloService (class and namespace both are HelloService) and has two endpoints (http and net.tcp) for the service.


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService">
        </endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

Saturday, March 22, 2014

system.web vs system.webserver

I have not worked in IIS6 and mostly I have worked on IIS 7+ and this came up when I was following a very old article (2003) ASP.Net article.

Anyway system.web is IIS6 and classic mode.
system.webserver is IIS7+  and integrated mode.

One scenario you might come across is when adding httpmodules to your ASP.Net project. If you want backward compatibility then you should add the module section to both system.web section (which is there by default for all ASP.Net projects ) and to system.webserver.

<system.web>
    <httpModules>
    </httpModules>
</system.web>

And

<system.webServer>
    <modules>
    </modules>
</system.webServer>