Introduction
In my previous article, we have seen the web role in Azure cloud service. In this article, I’m going to talk about Worker Role in Azure Cloud Service.
Content
- Difference between Web and worker role in Azure Cloud Service
- Creating a Worker Role
- Deploying the Worker Role
Difference between Web and Worker Role
The basic difference between the web and worker role is web role support and runs on IIS (Internet Information Service), where the worker role doesn’t support IIS.
The web role will accept the requests from the user and pass them to worker role instance for processing
Creating a worker role
In my previous article, I have explained about web role in cloud service. Now, I’m going to add the worker role project in the same existing web role project (AzureCloudServiceDemo).
Right click on Roles ->Add ->New Worker Role Project - > Worker Role with the service bus queue.
Please, go through my previous article where I have explained about the Azure Service bus queue.
The worker role is a simple class library project, by default, it consists of the class file which inherits the RoleEntryPoint class
The file consists of three overridden methods
- OnStart()
It is the first method will execute when the worker role starts, In this method I have written a code to create an Azure service bus queue if it's not available, then send a message to the queue. - Run()
This method will get fired once the Onstart is completed, I used this method to read the message from queue and send an email - OnStop()
Finally this method will get executed. In this method the service bus connection will be closed
- public override bool OnStart()
- {
- // Set the maximum number of concurrent connections
- ServicePointManager.DefaultConnectionLimit = 12;
- // Create the queue if it does not exist already
- string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
- var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
- if (!namespaceManager.QueueExists(QueueName))
- {
- namespaceManager.CreateQueue(QueueName);
- }
- // Initialize the connection to Service Bus Queue
- Client = QueueClient.CreateFromConnectionString(connectionString, QueueName);
- BrokeredMessage message = new BrokeredMessage("Am from Azure Service bus Queue as a worker roles");
- Client.SendAsync(message);
- return base.OnStart();
- }
- public override void Run()
- {
- string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
- var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
- Client = QueueClient.CreateFromConnectionString(connectionString, QueueName);
- BrokeredMessage message = Client.Receive();
- string body = message.GetBody<string>();
- using (MailMessage mm = new MailMessage("from address", "to address "))
- {
- mm.Subject = "Testing Worker Role";
- mm.Body = body;
- mm.IsBodyHtml = false;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- smtp.EnableSsl = true;
- NetworkCredential NetworkCred = new NetworkCredential("user name", "password");
- smtp.UseDefaultCredentials = true;
- smtp.Credentials = NetworkCred;
- smtp.Port = 587;
- smtp.Send(mm);
- }
- message.Complete();
- message.Abandon();
- }
- public class WorkerRole : RoleEntryPoint
- {
- // The name of your queue
- const string QueueName = "ProcessingQueue";
- // QueueClient is thread-safe. Recommended that you cache
- // rather than recreating it on every request
- QueueClient Client;
- ManualResetEvent CompletedEvent = new ManualResetEvent(false);
- public override void Run()
- {
- string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
- var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
- Client = QueueClient.CreateFromConnectionString(connectionString, QueueName);
- BrokeredMessage message = Client.Receive();
- string body = message.GetBody<string>();
- using (MailMessage mm = new MailMessage("from address", "to address "))
- {
- mm.Subject = "Testing Worker Role";
- mm.Body = body;
- mm.IsBodyHtml = false;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- smtp.EnableSsl = true;
- NetworkCredential NetworkCred = new NetworkCredential("user name", "password");
- smtp.UseDefaultCredentials = true;
- smtp.Credentials = NetworkCred;
- smtp.Port = 587;
- smtp.Send(mm);
- }
- message.Complete();
- message.Abandon();
- }
- public override bool OnStart()
- {
- // Set the maximum number of concurrent connections
- ServicePointManager.DefaultConnectionLimit = 12;
- // Create the queue if it does not exist already
- string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
- var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
- if (!namespaceManager.QueueExists(QueueName))
- {
- namespaceManager.CreateQueue(QueueName);
- }
- // Initialize the connection to Service Bus Queue
- Client = QueueClient.CreateFromConnectionString(connectionString, QueueName);
- BrokeredMessage message = new BrokeredMessage("Am from Azure Service bus Queue as a worker roles");
- Client.SendAsync(message);
- return base.OnStart();
- }
- public override void OnStop()
- {
- // Close the connection to Service Bus Queue
- Client.Close();
- CompletedEvent.Set();
- base.OnStop();
- }
- }

Figure 3: New queue(Processing queue) is created



Join the conversation! Your thoughts help the community grow.