Debugging Sharepoint timer jobs
Asked Answered
S

3

8

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...

Saturn answered 12/6, 2009 at 8:22 Comment(0)
A
9

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.

Awad answered 12/6, 2009 at 9:17 Comment(6)
It seems to be kinda working now... CLeaned solution and restarted timer, debugger is working...Saturn
Great! Debugging TimerJobs caused me some headache too.Awad
Interesting idea to through the assertion... also if you use Debug.Assert instead of Trace.Assert then any release build you do won't have that code (though at somepoint in your debug build you would want to comment it out anyway).Dyche
Just a side note: System.Diagnostics.Debugger.Break() should work aswell, its designed to pause the application and ask the user to attach the debugger.Instantaneity
Thanks for these info. You might be right, that Trace.Assert isn't the best solution.Awad
What do you know... System.Diagnostics.Debugger.Break(); works for web parts to. No more figuring out which w3wp is the correct one.Bethea
D
4

Debugger.Launch()

Drumfish answered 14/6, 2009 at 12:38 Comment(0)
D
2

Most common reasons:

  1. Did you do a debug build?
  2. Did you place the .pdb file in the same folder as the assembly? (A simple search should explain how - eg. Debugging Timer Jobs)
  3. Also read Debugging Custom Timer Jobs on MSDN for some tips.
  4. Perform the 3 R's: Re-build & Re-deploy assembly & Reset the Timer Service before trying to attach again.
Dyche answered 12/6, 2009 at 8:55 Comment(3)
Added the Debug.Assert = false, restarted my machine, but OWSTimer is not loading my pdb (I even copied it to the c:\windows\assemly\GAC_MSIL\<assemblyname>\<version> folder????Saturn
Rebuilt the project, removed the assebly manually with windows explorer, then copied the file manually also with the explorer, then copied pdb again, and debug does hook into process....Saturn
Apologies forgot to mention the usual rebuild/redoploy/restart process, will edit answer :)Dyche

© 2022 - 2024 — McMap. All rights reserved.