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.
Figure 1
Complete structure of my solution as shown below,
Figure 2
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
OnStart() in WorkerRole.cs
- public override bool OnStart()
- {
-
- ServicePointManager.DefaultConnectionLimit = 12;
-
-
-
- string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
- var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
- if (!namespaceManager.QueueExists(QueueName))
- {
- namespaceManager.CreateQueue(QueueName);
- }
-
-
- 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();
- }
Run() in WorkerRole.cs
- 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();
-
- }
WorkerRole.cs
- public class WorkerRole : RoleEntryPoint
- {
-
- const string QueueName = "ProcessingQueue";
-
-
-
- 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()
- {
-
- ServicePointManager.DefaultConnectionLimit = 12;
-
-
-
- string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
- var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
- if (!namespaceManager.QueueExists(QueueName))
- {
- namespaceManager.CreateQueue(QueueName);
- }
-
-
- 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()
- {
-
- Client.Close();
- CompletedEvent.Set();
- base.OnStop();
- }
- }
Let's run the application. You can see the Azure service bus queue is created for respective namespaces, and the message in the queue will be sent as an email through the run module in worker role.
Figure 3: New queue(Processing queue) is created
Figure 4: Message send as an email
Deploy Worker Role
Right click on the Cloud service project -> publish, the Microsoft Azure publish setting window will be opened, configure it (which is already discussed in my previous
article).
Figure 5
Once the publishing is done, you can check in the portal that the worker role will be deployed in the respective cloud service, as shown in the below figure.
Figure 6
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.