Binding to Blobs, Queues and Tables with Azure WebJobs

Azure WebJobs are awesome and you should start using them right now! I've been using them for a while and thought I'd share some knowledge:

Naming is important

Looking in the Azure WebJobs Dashboard, you'll have much better experience if your class and method names describe what their intention is: Rename your Functions.ProcessQueueMessage to something legible (especially if your web job has many triggers):


If you, in your dashboard, get a notice of a missing connection string, you'll want to add that in your site's Application settings:




Async Task is your friend

My job classes are static and my methods are too. Further, my methods are async Task so that I can use other async APIs with minimal friction:


You can bind your method parameters to everything storage (and some Service Bus too!)
As you could see in the example above (and also in Troy's article linked in the beginning), running a web job method in response to a blob being written, is trivial. In the code example, we also wrote to a storage queue asynchronously, through the IAsyncCollector interface. If we want to write an object to a queue, we can easily serialize it with Newtonsoft's JsonConvert.

Now, check this out:


You can use nested classes to describe your payloads (if you are not going to reuse them elsewhere, of course - in that case, put them somewhere so that you DRY). Annotating your messages like this, you can bind properties on the payload with resources you might need. In the example above, we bind our payload's PackageId property to the RowKey of our Packages Table, ensuring that we receive the appropriate details when we are about to process the request. To make things crystal clear, this is what the table might look like:


Pretty sweet, huh? WebJobs are awesome. Have fun!

Documentation: https://azure.microsoft.com/en-us/documentation/articles/websites-webjobs-resources/

Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints