Thursday, 20 August 2015

Getting Started for RabbitMQ

Thinks todo before start:
  • RabbitMQ server must be installed
  • If you want to install RabbitMQ, the Erlang/OTP must be installed in your machine
  • RabbitMQ service must be start before run the application
  • RabbitMQ.Client package must be install on your corresponding project

How to start the RabbitMQ service:

There are two ways to start the RabbitMQ service on your machine

  • The one is go to service.msc  and start the RabbitMQ service
  • Another one is All Programs > RabbitMQ server > RabbitMQ  Service Start

 If you are not start the RabbitMQ service:

ConnectionFactory for RabbitMQ client is not able to create the connection
ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.HostName = hostName;
            // this will fail If you are not start the RabbitMQ service.
            Connection = connectionFactory.CreateConnection();

Major namespaces, interfaces and classes:

using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;

The core API interfaces and classes are:

  • Imodel
  • Iconnection
  • ConnectionFactory
  • IBasicConsumer

Basic properties for ConnectionFactory to create connection are:

  • UserName
  • Password
  • HostName
  • VirtualHost
Create the connection:

protected IModel Model;
protected IConnection Connection;
protected string QueueName;

ConnectionFactory factory = new ConnectionFactory();
factory.UserName = “guest”;
// "gue
factory.Password = “guest”;
factory.VirtualHost = “/”;
factory.HostName = “hello”;
Connection = connectionFactory.CreateConnection();
Model = Connection.CreateModel();
Model.QueueDeclare(QueueName, false, false, false, null);

Publishing Messages:

IBasicProperties basicProperties = Model.CreateBasicProperties();
Model.BasicPublish("", QueueName, basicProperties, message);

Retrieving Messages:

Create Consumer  to retrieving the messages

QueueingBasicConsumer consumer = new QueueingBasicConsumer(Model);
            String consumerTag = Model.BasicConsume(QueueName, false, consumer);
    RabbitMQ.Client.Events.BasicDeliverEventArgs e =                                    (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue();
                    IBasicProperties props = e.BasicProperties;
                    byte[] body = e.Body;
                    // ... process the message
                    Model.BasicAck(e.DeliveryTag, false);
                catch (OperationInterruptedException ex)
                    // The consumer was removed, either through
                    // channel or connection closure, or through the
                    // action of IModel.BasicCancel().

Friday, 12 June 2015

How to create a KML file in C#


KML is a file format used to display geographic data in an Earth browser such as Google Earth, Google Maps, and Google Maps for mobile. KML uses a tag-based structure with nested elements and attributes and is based on the XML standard. All tags are case-sensitive and must appear exactly as they are listed in the KML Reference. The Reference indicates which tags are optional. Within a given element, tags must appear in the order shown in the Reference.

Sample code to create KML file in C#:

Sample cordinate values:

using System;
using System.Text;
using System.Net;
using System.Xml;
// Parameters , featureType, cordinateValues
// featureType(either Point OR Polyline)
// cordinateValues(longitude and latitude)
// FileDownload(To dowload a file)

        public ActionResult CreateKMLFile(string featureType, string cordinateValues)
            var kml = new XmlTextWriter(HttpContext.Response.OutputStream, Encoding.UTF8);

            //Use indentation for readability
            kml.Formatting = Formatting.Indented;
            kml.Indentation = 3;


            kml.WriteStartElement("kml", "");
            kml.WriteElementString("name", "Title here");
            kml.WriteElementString("description", "Description here");

            kml.WriteElementString("color", "ff00ff00");
            kml.WriteElementString("width", "3");
            kml.WriteEndElement(); // <Style>
            kml.WriteEndElement(); // <LineStyle>

            if (featureType == "polyline")

            if (featureType == "point")

            kml.WriteElementString("coordinates", cordinateValues);

            kml.WriteEndElement(); // <polyline>
            kml.WriteEndElement(); // <Placemark>
            kml.WriteEndElement(); // <kml>


            return File(new UTF8Encoding().GetBytes(kml.ToString()), "application/",

Tuesday, 9 June 2015

Using XMLHttpRequest in JavaScript


 XMLHttpRequest makes sending HTTP requests very easy. You simply create an instance of the object, open a URL, and send the request. The HTTP status of the result, as well as the result's contents, are available in the request object when the transaction is completed. This page outlines some of the common and even slightly obscure use cases for this powerful JavaScript object.

Sample code:

function sendRequest(url,callback,postData) {
            var req = createXMLHTTPObject();
            if (!req) return;
            var method = (postData) ? "POST" : "GET";
            if (postData)
            req.onreadystatechange = function () {
                        if (req.readyState != 4) return;
                        if (req.status != 200 && req.status != 304) {
//                                  alert('HTTP error ' + req.status);
            if (req.readyState == 4) return;

var XMLHttpFactories = [
            function () {return new XMLHttpRequest()},
            function () {return new ActiveXObject("Msxml2.XMLHTTP")},
            function () {return new ActiveXObject("Msxml3.XMLHTTP")},
            function () {return new ActiveXObject("Microsoft.XMLHTTP")}

function createXMLHTTPObject() {
            var xmlhttp = false;
            for (var i=0;i<XMLHttpFactories.length;i++) {
                        try {
                                    xmlhttp = XMLHttpFactories[i]();
                        catch (e) {
            return xmlhttp;


Now the file file.txt is fetched, and when that's done the functionhandleRequest() is called. This function receives the XMLHttpRequest object as an argument, which I traditionally call req (though, of course, you can use any variable name you like). Typically, this function reads out the responseXML orresponseText and does something with it.

function handleRequest(req) {
            var writeroot = [some element];
            writeroot.innerHTML = req.responseText;

This function creates a new XMLHttpRequest object for every request you make. In simple cases such as this site, where every page fetches only three to five files, I don't mind creating three to five objects. In more complex sites, however, where any page can make an arbitrary amount of requests, it's probably better to write a function that reuses existing XMLHttpRequest objects.