Debugging Sharepoint timer jobs

sharepointtimer-jobswss

I am creating my first Timer Job and want to debug it. I have installed the timer job through a feature, and added it to a webapplication's JobDefinitions collection, and added a SPMinuteSchedule to run every 5 minutes (for testing purposes).

Then, in Visual Studio I've tried attaching the debugger to the WebApplication's Process, the Central Admin process and the OWSTIMER.exe process, but it will not debug into the Execute Method of the TimerJob. What am I missing here.

P.S. The timer job status says succeeded, so it is running. Weird…

Best Answer

When I'm debugging TimerJobs I insert a Assertion at the very first begin of the Execute method, which will always fail. This results in a pop up every time the Execute method is called so you can be sure that the TimerJob was started and have enough time to attach the debugger. Of course you have to remove the Assertion before you go live.

System.Diagnostics.Trace.Assert(false);

One more important thing is that you restart the Timer Service after you deployed a new DLL. Otherwise the Timer Service will run the TimerJob from the old DLL.

Related Topic