Yesterday I posted up some thoughts about how to streamline deployment of .NET-based Web Jobs along with your web-application. Continuing on with Web Jobs I thought it might be interesting to figure out how to debug them once they are deployed. With Windows Azure it is now possible to remotely debug a web application via Visual Studio when it is running in Windows Azure Websites.
Using the same mechanism it is also possible to attach to a running Web Job process which can help you dig into problems in Web Job execution during development (I wouldn’t recommend attaching a debugger on a production site). Unfortunately the process is a little kludgey right now so I’m not sure how effective this would be as a “normal-case” debugging scenario, but it’ll work in a pinch.
Before your start, to help you with the timing issue you might like to insert a Thread.Sleep(…) into your job just to give you enough time to attach to the process. Once you’ve got that updated Web Job code deployed start the debugger by attaching to the Windows Azure Website via the Attach Debugger menu option in Server Explorer.
Visual Studio will then spin away loading symbols etc. Once that is done go into the Windows Azure management console and start the web-job. At this point you need to work fast (depending on the value of the Thread.Sleep(…) that you put in.
Go into the “Debug | Attach to Process” menu item and select the web-site you are debugging from the Qualifier drop down (this is the remote debugging session that is currently supporting your web-app debugging). What you should see is your web-job executable in the list of available processes.
Once the debugger is attached any breakpoints that you have set within the web-job code will be hit and you can step through line by line.
Bingo! So now you know how to debug Web Jobs remotely via Visual Studio. I’d really like to see this process streamlined a bit so that when you attach a debugger to the web-site it automagically will hit breakpoints in the web-jobs that are part of the overall deployment.