Saturday, January 24, 2015

Ghostery Makes it easy for you

Ghostery is such a handy tool that makes our life easy when it comes to security. You can find and able to block all external sources or third party analytics that fetches source data from your website.

Ghostery is an add-ons available for chrome, safari, firefox. Check out cool stuff.

Convert Timezone UTC to Local Time e.g TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")


Convert UTC to Local TimeZone

 
public class TimeZoneConvertorHelper
    {
        private static readonly TimeZoneInfo IndiaTimeZone;

        static TimeZoneHelper()
        {
            IndiaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");       
        }

        public static DateTime GetCurrentMelbourneDateTime()
        {
            return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, IndiaTimeZone);
        }
    }

Big Debate: Sticky Session

Sticky Session

Cautions decision to be taken when enabling sticky session . Checks to be made:-
1. Load balanced server
2.Users session distribution across load balanced server.
3. Round Robin distribution
 

Warnings

  1. When sticky session enabled , requests are not evenly distributed.
  2. Sticky sessions are always good if there is not much session values/data for each users.
  3. RAM utilization may be high as compared to CPU
Options:
Cloud : Elastic Load balancing
Enable sessions based on application instances or node.

Web API :Utility To read Json output From WebResponse


Introduction

The below utility fetches the output from API request through HTTP in JSON format.


public static class JsonRequestStreamUtility
    {
        public static WebRequest CreateRequest(string url, string method = WebRequestMethods.Http.Get, string body = null)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = method;
            webRequest.Accept = "application/json";
            webRequest.Headers.Add("Accept-Charset", "utf-8");
            webRequest.ContentType = "application/json";

            webRequest.Proxy = new System.Net.WebProxy()
                {
                    Address = new Uri("xyz"),
                    UseDefaultCredentials = true,
                };
            
            if (body == null)
                return webRequest;

            using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
            {
                streamWriter.Write(body);
            }
            return webRequest;
        }

        public static string ExecuteJsonResponseStream(WebRequest webRequest)
        {
            
            if (webRequest == null) throw new ArgumentNullException("webRequest");

              try
            {

                // Get the stream associated with the response.
                Stream receiveStream = webRequest.GetResponse().GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format. 
                StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
                return  readStream.ReadToEnd();
                
            }
            catch (WebException we)
            {
                              
                return string.empty;
            }
        }
    }