Tuesday, March 17, 2015

Starting Scala & R Language

Its always good to learn at least one new programming language per year to sustain in software engineering industry as architect or in any development position. At least need to know what are the capabilities and some hands on experience on the new language we are learning every year.

Last year I was with PowerShell. It is a nice language. Less code more things. That is the beauty if it. Also piping is a great feature. Seamless integration with .Net programs etc...

This year I decided to learn something not much related to .Net. One is Scala and other is R language.

Why interested in Scala & R as a hard core .Net guy?

Let us see some code fragments in these languages.

//Scala
  def OddOrEven(a: Int) = {
    if (a % 2 == 0) "Even"
    else "Odd"
  }

There is not more things interesting here except the absence of return statement. Yes the language now can understand programmer's intention and can remove strict rules.

This is just a start. There are more things out there in scala which makes programming faster with less code.

After working in delivery oriented organization which maintains huge amount of legacy code, I feel the way to survive in intense delivery environment is to stick to the below principle

"Less code less defects"

Some of the features are already available in C# in the form of lambda expressions, automatic properties etc...But Scala seems more simplified and takes advantage of great JVM.

# R Program to increment all members in list
a=c(10,12,13)
a=a+1
print(a)

The same thoughts here. Less code as well as R is very good for statistical programming.

There are many more reasons why these languages are becoming popular. Just google for more features.

Setting up the environment for these 2 languages is very easy. Next time I will post it.

Tuesday, March 10, 2015

Using ref parameters in WCF service contract

Actually it didn't make sense to have ref parameter in service calls as most of the cases service runs in different process and client don't get reference to the server memory address. But still WCF supports it.

I never thought it will work until I saw this in one of the production applications. We were re-factoring a legacy code base and could see one operation contract with ref keyword. Initially we thought that service itself is not in use and can delete it. But my colleague was sure that it works. So decided to do a sample as running the app and finding out which functionality in that giant app is going to use it takes more time. Below is the PoC we did.

WCF Service contract

Created service contract with 3 operation contracts

    [ServiceContract]
public interface IMathServiceWithRef
{
    [OperationContract]
    int GetSum(int number1, int number2);
    [OperationContract]
    Result GetSumAndAverageAsResultObject(int number1, int number2);
    [OperationContract]
    int GetSumAndAverage(int number1, int number2, ref int average);
}
[DataContract]
public class Result
{
    [DataMember]
    public int Sum { getset; }
    [DataMember]
    public int Average{ getset; }
}

WCF Service Implementation

Simple implementation as below

public class MathServiceWithRef : IMathServiceWithRef        
{
    int IMathServiceWithRef.GetSum(int number1, int number2)
    {
        return number1 + number2;
    }
    Result IMathServiceWithRef.GetSumAndAverageAsResultObject(int number1, int number2)
    {
        return new Result() { Sum = number1 + number2, Average = (number1 + number2) / 2 };
    }
    int IMathServiceWithRef.GetSumAndAverage(int number1, int number2,ref int average)
    {
        average = (number1 + number2) / 2;
        return number1 + number2;
    }
}

WCF Client proxy & Test class

Right clicked on the service reference section and added using the svc url. Used simple test project which already have service reference

        [TestMethod()]
public void WithNormalValues_ShouldSuccess()
{
    WCFService1.MathServiceWithRefClient client = new WCFService1.MathServiceWithRefClient();
    int average=0,sum = 0;
    //////////////////////////////////////////
    sum = client.GetSum(20, 10);
    Assert.IsTrue(sum == 30);
    //////////////////////////////////////////
    Result result = client.GetSumAndAverageAsResultObject(20, 10);
    Assert.IsTrue(result.Average == 15 && result.Sum == 30);
    /////////////////////////////////////////
    sum=client.GetSumAndAverage(20, 10, ref average);
    Assert.IsTrue(average == 15 && sum==30);            
}

And this worked as expected. Fiddler results below

Fiddler results

Simple Sum() call

Request XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <GetSum xmlns="http://tempuri.org/">
      <number1>20</number1>
      <number2>10</number2>
    </GetSum>
  </s:Body>
</s:Envelope>
Response XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <GetSumResponse xmlns="http://tempuri.org/">
      <GetSumResult>30</GetSumResult>
    </GetSumResponse>
  </s:Body>
</s:Envelope>

With ref keyword

Request XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <GetSumAndAverage xmlns="http://tempuri.org/">
      <number1>20</number1>
      <number2>10</number2>
      <average>0</average>
    </GetSumAndAverage>
  </s:Body>
</s:Envelope>
ResponseXML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
      <GetSumAndAverageResponse xmlns="http://tempuri.org/">
        <GetSumAndAverageResult>30</GetSumAndAverageResult>
        <average>15</average>
      </GetSumAndAverageResponse>
    </s:Body>
  </s:Envelope>

As we can see in the fiddler the response xml just contains the ref value. But not as result. 

WCF communication protocols tested

Obviously there is no need to test using various protocols as WCF is more towards protocol independent in all most all the cases.

Still we tested in net.pipe and net.tcp protocols and it worked.

Why someone should use ref keyword in WCF operation contact

If someone is really lazy to create a DataContract class they can use this method. I don't see any other benefits.

Happy coding. 

Tuesday, March 3, 2015

Is my code running inside virtual machine?

This is a small tip which rarely, we as programmers need to apply. Its about how to determine whether our code is running inside a virtual machine or not.Actually it doesn't matter if we are writing business applications. But in some cases especially when we are dealing with hardware devices we may need to disable some features by checking whether the code is under virtual machine. Also this is not a first thing any programmer is looking for. So I am aggregating links as well.

Native C++

Hope everyone will be aware of this as people who are in unmanaged world are talking directly to the computer.

.Net (C# or VB.Net)

In .Net the easiest way if to query the WMI objects. We can get easily get example code snippets from internet. Some are below.

http://stackoverflow.com/questions/498371/how-to-detect-if-my-application-is-running-in-a-virtual-machine

SQL

Query the DMV sys.dm_os_sys_info as follows

SELECT virtual_machine_type,virtual_machine_type_desc 
FROM sys.dm_os_sys_info

The sys.dm_os_sys_info gives much more information about the host system such as last SQL Server restart time, memory information etc...

There are different virtualization softwares available to create virtual machines. The method to determine that detail may vary from type to type. Need to invest some time to determine that.