Azure WebJobs : Description & Implementation

Vaibhav Bhapkar
5 min readDec 23, 2019

Description:

One of the most common needs for web application is able to run background tasks. Such as batch processing, scheduled tasks, and long-running processes are common in modern applications. Webjobs have built-in triggers for a number of different events inside of Azure: storage queues, blobs, service bus queues, topics, and schedule triggers.

Webjobs provides an easy way to run scripts or programs as background processes in the context of the app service web app, API app, or mobile app. Using Azure Webjobs you can upload and run different file formats (cmd, bat, exe, ps1, sh, php, py, js and jar).

In order to run a Webjobs you will need an already running app service plan this will be added into your existing hosting plan only and no additional cost involved with it. You can create multiple Webjobs in-app service plan. Deploying the Webjobs is also quite easy since they deploy using the same infrastructure as any app service this allows deploying from source control or FTP or from visual studio.

There may be a requirement when you want to move your cloud data to on-premise and the same way move on-premise data to cloud in this case you need a job which will do your processing of reading data from cloud database table then convert it to excel and then store into created blob container on the cloud. We will schedule a Webjob for this task reading data from the table then convert to excel and store it in a blob container.

Step 1) Create Resource Group:

Azure Portal -> All Resources -> Resource Group -> Create New

Step 2) Create MySQL Database On Azure.

Azure Portal -> All Resources -> Azure database for MYSQL -> Create New

Once deployment of Azure MYSQL database completed then you check details about server name and user name in the overview section.

To access and connect to this MYSQL server from MySQL workbench you need to modify/add policies in the connection security section.

After adding the required policy, you can connect using MySQL workbench by entering specifically required credentials.

Step 3) Create Storage account

Azure Portal -> All Resources -> Storage Account -> Create New

Once you created the storage account next step is to create a blob container inside your container to store the file.

Select Container -> Create New -> Select Blob option

Storage account name and access keys to access it will be available in the Access Keys section also in the Access control section you can assign role access policies with respect to that storage account.

Step 4) Create AppService where you can deploy your Webjob

Azure Portal -> All Resources -> App Service -> Create New

Once the app service is created you can navigate to web job inside app service and create a new job.

Here, the file upload option will include your console application release build zip file and based on your need you can schedule this job either of continuous type or triggered type and instance also vary manual one on scheduled one.

Once this process is completed you can run this Webjob and you can see your file will be uploaded in azure blob storage.

Source code to perform this operation:

using DocumentFormat.OpenXml;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Spreadsheet;using Microsoft.WindowsAzure.Storage.Auth;using Microsoft.WindowsAzure.Storage.Blob;using MySql.Data.MySqlClient;using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.IO;using System.Linq;using System.Threading.Tasks;using System.Web;namespace TestAzureJob{public class Helper{private static readonly StorageCredentials cred = new StorageCredentials(“Account name”, “Key”);private static readonly CloudBlobContainer container = new CloudBlobContainer(new Uri(“https://storage account name.blob.core.windows.net/conatinername “), cred);private static readonly string connectionStr = “Azure connection string”;private static readonly string directoryPath = $”{ AppDomain.CurrentDomain.BaseDirectory}\\Downloads”;private static readonly string excelName = “Inventory.xlsx”;private static readonly List<string> columns = new List<string>() { “id”, “name”, “quantity” };private static readonly string tableName = “inventory”;public static string DBExportToExcel(){string result = string.Empty;try{//Get datatable from dbDataSet ds = new DataSet();MySqlConnection connection = new MySqlConnection(connectionStr);MySqlCommand cmd = new MySqlCommand($”SELECT {string.Join(“,”, columns)} FROM {tableName}”, connection);using (MySqlDataAdapter adapter = new MySqlDataAdapter(cmd)){adapter.Fill(ds);}//Check directoryif (!Directory.Exists(directoryPath)){Directory.CreateDirectory(directoryPath);}// Delete the file if it existsstring filePath = $”{directoryPath}//{excelName}”;if (File.Exists(filePath)){File.Delete(filePath);}if (ds.Tables.Count > 0 && ds.Tables[0] != null || ds.Tables[0].Columns.Count > 0){DataTable table = ds.Tables[0];using (var spreadsheetDocument = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook)){// Create SpreadsheetDocumentWorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();workbookPart.Workbook = new Workbook();var sheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>();var sheetData = new SheetData();sheetPart.Worksheet = new Worksheet(sheetData);Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());string relationshipId = spreadsheetDocument.WorkbookPart.GetIdOfPart(sheetPart);Sheet sheet = new Sheet() { Id = relationshipId, SheetId = 1, Name = table.TableName };sheets.Append(sheet);//Add header to sheetDataRow headerRow = new Row();List<String> columns = new List<string>();foreach (DataColumn column in table.Columns){columns.Add(column.ColumnName);Cell cell = new Cell();cell.DataType = CellValues.String;cell.CellValue = new CellValue(column.ColumnName);headerRow.AppendChild(cell);}sheetData.AppendChild(headerRow);//Add cells to sheetDataforeach (DataRow row in table.Rows){Row newRow = new Row();columns.ForEach(col =>{Cell cell = new Cell();//If value is DBNull, do not set value to cellif (row[col] != System.DBNull.Value){cell.DataType = CellValues.String;cell.CellValue = new CellValue(row[col].ToString());}newRow.AppendChild(cell);});sheetData.AppendChild(newRow);}result = $”Export {table.Rows.Count} rows of data to excel successfully.”;}}// Write the excel to Azure storage containerusing (FileStream fileStream = File.Open(filePath, FileMode.Open)){Task<bool> exists = container.CreateIfNotExistsAsync();var blob = container.GetBlockBlobReference(excelName);blob.DeleteIfExistsAsync();blob.UploadFromStreamAsync(fileStream);}}catch (Exception ex){result = $”Export action failed. Error Message: {ex.Message}”;}return result;}}}

Thank you see you in the next article!!

You can reach out to me here,

LinkedIn: https://www.linkedin.com/in/vaibhav-bhapkar

Email: vaibhavbhapkar.medium@gmail.com

--

--

Vaibhav Bhapkar

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *