Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites. What Scheduling Options is supported by Microsoft Azure WebJobs?
Azure WebJobs can run Continuously, On Demand or on a Schedule.
In what language/scripts are WebJobs written? Azure WebJobs can be created using the following scripts:
In Visual Studio 2013 Update 3, WebJobs can be created and deployed directly from Visual Studio itself.
To get started, just create a new project, Under C#, select, Microsoft Azure WebJobs. In this example, a "HelloTNWiki" project is created.
class
Program
{
static
void
Main()
Console.WriteLine(
"Hello TN Wiki!"
);
}
To deploy the WebJob, right-click on the project and select Publish as Azure WebJob.
You shall be required to sign-in and enter your Microsoft Azure credentials
Once signed in, you may select in which WebSite the Job Shall runs.
Click on OK, then select Publish.
Now, if you go the the Microsoft Azure portal, and navigate to your WebSite, you shall see a new WebJob created and deployed.
In Visual Studio, create a Console Application.
The entry point of the application is still Main(). This example shall just display "Hello TN Wiki!".
The following steps needs to be performed to deploy the WebJob:
To run a on-demand WebJob as explained above, select the job and click on Save at the bottom of the page.
The WebJobs dashboard in the Azure management portal provides powerful management capabilities that give you full control over the execution of WebJobs, including the ability to invoke individual functions within WebJobs.
The dashboard also displays function runtimes and output logs.
To access the dashboard page, click on the link under the Log Column.
This opens the dashboard page which contains details and statistics about the WebJob.
By selecting a specific run, statistics about the job together with the Job Log can be obtained.
As per the result of the example above, "Hello TN Wiki!" is displayed in the Job Log
The WebJobs SDK makes it easier to use Azure Storage.
The WebJobs SDK has a binding and trigger system which works with Microsoft Azure Storage Blobs, Queues and Tables as well as Service Bus Queues.
To use the WebJob SDK in Visual Studio, the following NuGet Package needs to be installed.
From the Azure Portal, Go to, Data Services, Storage, Quick-Create and fill in the required information
Once the storage account is provision, the next thing to do is to get the keys. Click on the Manage Keys button like below
Below are the steps to save a string to a blob using C# code
1. Build the connection string using the keys retrieved above
string
ConStr =
"DefaultEndpointsProtocol=https;AccountName= xxx ;AccountKey=xxx "
;
2. Create an instance of CloudStorageAccount. This Represents a Windows Azure Storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConStr);
Provides a client-side logical representation of the Windows Azure Blob service. This client is used to configure and execute requests against the Blob service.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(
"blobcontainer"
container.CreateIfNotExists();
5. Gets a reference to a block blob in the container and Upload a string text to it.
CloudBlockBlob blob = container.GetBlockBlobReference(
"BlobFile.txt"
blob.UploadText(
"HelloTNWiki"
You may now view the created container in your Storage Account > Containers in the Azure Portal.
By clicking on he container, you will be able to see the newly created Blob and its contents
Having gone into the basics of Azure WebJobs, Below is an example where all the concepts can be used and applied.
Consider a scenario where a sales WebApp is being deployed into Azure. The App stores sales information from several branches of a store.
The requirement is everyday at a specific time in the morning, compile the total sales per region, save the report on a Blob storage and email the report to the directors
Assume the Database schema of the Application is like below
The code block below connects to an Azure Database, select and aggregates the sales information as required.
public
GetDailySales(
date)
dbconnect();
SalesDetails =
""
try
StrCommand = @"select region, sum(amount) AMOUNT
FROM SALES
WHERE TRANDATE=
'" + date + "'
group by region";
using
(SqlCommand command =
new
SqlCommand(StrCommand, con))
(SqlDataReader reader = command.ExecuteReader())
while
(reader.Read())
SalesDetails = SalesDetails + reader.GetString(0) +
"\t"
+ reader.GetInt32(1) +
"</br>"
con.Close();
catch
(Exception e)
"Could not retrieve Information "
+ e.Message);
throw
"Information Retrieved Successfully"
return
SalesDetails;
The next step is to send the results by mail. This is done by the function below
SendEmail(
msg)
MailMessage mail =
MailMessage();
mail.From =
MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = msg;
mail.IsBodyHtml =
true
(SmtpClient smtp =
SmtpClient(smtpAddress, portNumber))
smtp.Credentials =
NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
"Email sent to "
+emailTo +
" at "
+ DateTime.Now);
"Email failed "
The function below creates a container if it does not exist and save the result in a blob
WriteToBlob(
salesreport)
acs =
"DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx"
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(acs);
"salesinfo"
"DailyReport"
+ DateTime.Now +
".txt"
blob.UploadText(salesreport);
"File saved successfully"
); ;
"Saving to blob failed "
Notice the use of Console.WriteLine() in each function. This shall help to trace the execution process and log errors in case of problems.
Thus the job log will be like below
1. http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/ 2. http://azure.microsoft.com/en-us/documentation/articles/websites-webjobs-resources/ 3. http://azure.microsoft.com/blog/2014/09/06/announcing-the-0-5-0-beta-preview-of-microsoft-azure-webjobs-sdk/ 4. http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.cloudstorageaccount.aspx 5. http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.aspx 6. http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblobclient.aspx
Great work . Thanks for share.