This is a hands-on lab for using Web Jobs with an advanced lab for using Azure SQL Database for those that finish early.
This is based on the blog post by Scott Hanselman. See also the Getting Started with the Web Jobs SDK post.
- Create a new console application in Visual Studio
- Add a reference to
System.Drawing 4.0.0.0
Install-Package nquant
Install-Package Microsoft.WindowsAzure.Jobs.Host -pre
- Change
Program.cs
to include this:using Microsoft.WindowsAzure.Jobs; using nQuant; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { JobHost host = new JobHost(); host.RunAndBlock(); } public static void SquishNewlyUploadedPNGs( [BlobInput("input/{name}")] Stream input, [BlobOutput("output/{name}")] Stream output) { var quantizer = new WuQuantizer(); using (var bitmap = new Bitmap(input)) { using (var quantized = quantizer.QuantizeImage(bitmap)) { quantized.Save(output, ImageFormat.Png); } } } } }
- Build the solution
- Go to
bin\Debug
and zip up all the files up - Go to the WebJobs tab for a Web Site in the Windows Azure portal
- Click
New
to create a new web job by uploading the zip file - Create a Windows Azure Storage Account if you haven't already
- Construct a connection string for your storage account
DefaultEndpointsProtocol=https;AccountName=<storage_account_name>;AccountKey=<storage_account_access_key>
- Go to the
Configure
tab for your Web Site in the Windows Azure portal and add the storage account connection string for theAzureJobsRuntime
andAzureJobsData
connection strings - Look at the status of the web job in the WebJobs tab of your Web Site and ensure it's set to Running
- Look at the logs and dashboard for your Web Job to monitor what happens when it's invoked
- Open up the storage account that you are using for the web job inside of Visual Studio
View > Server Explorer > Windows Azure > Storage
(or any other tool of choice) - Drop in sample.png to the input blob container
- Wait for the file to get processed into the output directory
One of the things you need to deal with when using Azure SQL Database is dealing with transient errors - to that end Rob Moore has created some demo code to illustrate the effects of transient errors. This lab involves playing around with that code to see how easy it is to connect to an Azure SQL Database (but keeping in mind the fact you have to use transient fault protection) and if you have the time then to actually exercise the transient errors. The codebase also demonstrates Azure Storage connectivity.
- Navigate to the demo site and follow the instructions to exercise the demo
- Initially, only deploy the site to one Windows Azure instance and study the code locally to try and understand how it works
- If you have time then do the full demo to try and see the transient errors happen (as explained in the demo site)