Friday, December 13, 2013

What is the way to avoid memory leak and faulted state when using block to call WCF client ?

DO NOT USE using block:

try
{
    client.Close();
}
catch (CommunicationException e)
{
    client.Abort();
}
catch (TimeoutException e)
{
    client.Abort();
}
catch (Exception e)
{
    client.Abort();
    throw; //Make sure to keep original exception 
}

http://msdn.microsoft.com/en-us/library/aa355056.aspx

Wednesday, December 11, 2013

How to make a MD5 hash from a string?

        public string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

Reference:
http://blogs.msdn.com/b/csharpfaq/archive/2006/10/09/how-do-i-calculate-a-md5-hash-from-a-string_3f00_.aspx

What is BSON

Stand for Bin­ary JSON
It is Lightweight, Traversable and Efficient
http://bsonspec.org/