Skip to main content

Azure Service Bus Vs Azure Web Job

Azure Service Bus is a messaging infrastructure that sits between applications allowing them to exchange messages for improved scale and resiliency. It offers the following capabilities:
Queues: offers simple first in, first out guaranteed message delivery.
When to use Azure Service Bus?
Service Bus queues are a general-purpose technology that can be used for a wide variety of scenarios:
  • Communication between web and worker roles in a multi-tier Azure application
  • Communication between on-premises apps and Azure hosted apps in a hybrid solution
  • Communication between components of a distributed application running on-premises in different organizations or departments of an organization
There are also Topics and Relay capabiliteis but we will talk about them in the future.
A figure that illustrates how Azure Service Bus works.
There is a sender who send a message with some properties to the queue and then the queue may do some processing over the message and send it to a receiver.
    public class Program
    {
        public static string SericeBusConnectionString = "service_bus_connection_string";
        public static void Main(string[] args)
        {
            string queueName = "queue_name";
            var namespaceManager = NamespaceManager.CreateFromConnectionString(SericeBusConnectionString);
            if (!namespaceManager.QueueExists(queueName))
            {
                namespaceManager.CreateQueue(queueName);
            }
            QueueClient client = QueueClient.CreateFromConnectionString(SericeBusConnectionString, queueName);
            var message = new BrokeredMessage();
            message.Properties["first_name"] = "Test First Name";
            message.Properties["last_name"] = "Test Last Name";
            client.SendAsync(message);
        }
    }
Don't forget to replace the connection string in the code with your service bus connection string and you replace queue name with anything that can describe what does your queue do.
We can now send message to the service bus but we don't know when will a message be sent, It could be sent at any moment so how can we handle this?
Easy, we can handle that by using Azure WebJob, A webjob is a background service that can either be always running or run on schedule, but in the case of service bus, we will create one which will be running continuously because we never know when will a message be sent.
Let's create a console application that will be uploaded to azure as a webjob writing the code that will receive the service message bus.
    class Program
    {
        static void Main(string[] args)
        {
            string queueName = "queue_name";
            var _servicesBusConn = "servie_bus_connection_string";
            QueueClient client = QueueClient.CreateFromConnectionString(_servicesBusConn, queueName);
            while (true)
            {
                var msg = client.Receive();
                if (msg != null)
                {
                        string fn = msg.Properties["first_name"].ToString();
                        string ln = msg.Properties["last_name"].ToString();
                        string name = fn + " " + ln;
                        Console.WriteLine("Name = " + name);
                        Trace.TraceInformation("Name = " + name);
                        msg.Complete();
                    }
                }
            }
        }
    }
Now, we need to create a zip file for the project binaries to upload as a webjob, It's pretty easy to upload a webjob all what you need to do is uploading and choose run continuously.
Congratulations, You have already created a service bus queue and handled everything you need to.
Here is the sample of sender code and the receiver code.

Comments

Popular posts from this blog

MVC Interview Questions

MVC Interview Questions What is MVC Application life cycle ? What is default route in MVC? What is Output Caching ? What are Route Constraints in MVC? What are the Benefits of Area in MVC? How do you implement Forms authentication in MVC? What are the Main Razor Syntax Rules? Define attribute based routing in MVC? What are HTML helpers in MVC? What does the MVC pattern define with 3 logical layers? What are the advantages of MVC? List the various return types of a controller action method. What are the types of filters? What are the Filters in MVC? What are the three segments for routing important? What is Route in MVC? what is the difference between ViewData and ViewBag? attribute based routing in MVC? What is GET and POST Actions Types? What is MVC (Model view controller)? What is Validation Summary in MVC? What’s new in the latest version of MVC? How to render html in Asp.net MVC view? Explain the concept of MVC Scaffolding? What is Bundling and Mini...

Angular Step By Step Installation Process

# Angular Step By Step  Hello Techies,        In this post i am going to guide you to install Angular and create and run your first app. Please follow bellow steps.. Step 1 (Node NPM Installation) Node Js ( NPM)  should be installed on system. So for installation of Node Js follow bellow steps. Open  https://nodejs.org/en  .  Download stable Version (LTS) of Node js. After complete the installation open CMD and check the version. Step : 2 (Install Angular by NPM) Type " npm install -g @angular/cli "  2.  Select folder where you are going to create Angular Project.  3. Type ng new 'your app-name' EX:  ng new my-dream-app And Relax it will take some couple of minute to create app , because its downloading all the package from server by npm in you local system. Type : " cd my-dream-app " to select project which is created.  Now Type " ng serve " to run the project...