I have some fellow colleagues who are extremely talented but they don’t have much working experience with Azure. So each time they are assigned to a job where they need to interact with the azure for data, they kind of struggle. For example, few days earlier one of them is assigned to a job where he have to resize all the images. Writing the code which can resize the image is not that hard. He did that part in fair amount of time. But he find it bit difficult to interact with the storage. To help from such situation Azure blessed us with Webjob SDK.
Azure Webjob SDK is all about model binding in the cloud. Making it easier to talk to storage ! This is helpful and can speed up work because now I don’t have to write stuffs to establish connectivity with azure storage. Instead, I can concentrate on the core business logic part and let Webjob SDK to handle the connectivity issue for me. The best part of Webjob SDK is that it offers stuffs out of the box like triggering. So now I can do something more meaningful like do some task upon the arrival of new files or information on queue or so.
The first step is to install the nuget package for Azure Webjobs. Adding package from nuget is damn easy, I open up my nuget console and hit the following command :
Now, I need to initialize an object which will do all the heavy lifting for me, things like triggering, binding, invocation of function and so on. I can do that by initiating JobHost object. So, here is my entry point, canonical main function:
I press Ctrl+Shift+B to build the solution and everything seems fine. Then I have tried to run this solution. Boom! Now, whats that? Its a run time error! But I have not done anything except initializing the JobHost object. Whats causing the error?
When I look into the error message it says –
“Microsoft Azure WebJobs SDK Dashboard connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways:
- Set the connection string named ‘AzureWebJobsDashboard’ in the connectionStrings section of the .config file in the following format <add name=”AzureWebJobsDashboard” connectionString=”DefaultEndpointsProtocol=http|https; AccountName=NAME;AccountKey=KEY” />, or
- Set the environment variable named ‘AzureWebJobsDashboard’, or
- Set corresponding property of JobHostConfiguration.”
The error message is helpful as it also indicates how to resolve the issue. But wait a minute. I don’t have any plan to use storage yet, so why its asking me to set storage credential? Plus from my Azure Webjobs deployment experience, I know Azure storage is not required for Webjobs ! So, why it is issuing this error now?
Well, even though I don’t use storage entities, I still need to configure it because WebJobs’ SDK use some blobs and other storage entities for its own tracking and operation.
- AzureWebJobsStorage connection string is required.
- AzureWebJobsDashboard connection string is optional. you only need it if you want to be able to use the Dashboard UI for logs, etc.
Luckily, I already have an storage account which I am going to use at this point. If you don’t have one you can create storage account easily. Make sure the storage is placed the same region as the host website.
Now that I have provided the required storage information I will no longer face the existing runtime error. However, I know the template still need one more tweaking before it finally runs without an error. To find out the error lets re-run our simple program.
As you can see the exception saying “can’t be invoked from Azure WebJobs SDK. Is it missing Azue WebJobs SDK attributes?” Resolving this issue is quite easy. I just have to Use public on the main program class.
I took all the moving parts and placed them on the right spot and now everything seems workable. Now, I can focus on our canonical main function –
A JobHost is an execution container for your WebJob which knows what all functions you have in your program. The RunAndBlock() method will cause the JobHost to listen for any triggers on any functions that you define in this Host.
Before I move any further, PLEASE NOTE I am using WebJob 1.0.1. If you are using earlier version the code snippet that I am showing might not work for you!
The following code snippet has two methods.
- ProcessText() and
The method ProcessText() will be triggered when a text file will be found in the blob under the container named “input-dir” with a extension matches to “.txt”. After that it will write the file with the same name in “output-dir”.
Note : If you don’t have a container with the name “output-dir” in your blob then this method will create a container by itself.
CopyBlobToBlob() method works like the same way ProcessText() does. But since here I am pulling images so instead of TextReader, I am using Stream. The attribute BlogTrigger gets fired when a new blob content appears in the blob container. The Blob attribute write new blob contents to specified blob container.
Wrapping Up :
Azure Webjob SDK simplifies the communication part between your app and the cloud storage. So developers can focus on their main business logic and and they can leverage their app with the powerful binding features that Webjob SDK offers. They can simple pass proper attribute in their method parameter to connect to Azure Storage (notably blobs, tables, and queues) and SDK will handle everything.