Debugging CRM .net API dlls

1 minute read time.

So you're having difficulty debugging a .net dll with Sage CRM, you have followed all the instructions in the documentation for setting up debugging but it still doesn't work... Microsoft only knows why sometimes the debugger just will not catch your breakpoints... Well here is a sure-fire way of getting a debugging environment to catch your code:

System.Diagnostics.Debugger.Break();

This will cause the operating system to look for a debugger for the currently executing process. You will get a window like this:

You can now choose which debug environment you want to use. If you already have the project open you can select it from here also.

Note that once the debugger has been attached any breakpoints you have set should work.

One way to make sure this works for all your pages is to put it in the App Factory constructor like this:

public static class AppFactory
{
static AppFactory()
{
#if (DEBUG)
System.Diagnostics.Debugger.Break();
#endif
...
Now every time CRM launches a page from your .net dll it will break on this line, giving you the opportunity to attach a debugger.
  • Hi guys,

    Posting this in case someone gets the same problem as me. I am using VS 2010 and tried to debug my .net code by attaching to w3wp.exe process. However, no matter what I do it just doesn't load the symbols even though I had the pdb files in C:\Program Files (x86)\Sage\CRM\CRM\CustomDotNet. I managed to break the code using Jack's method System.Diagnostics.Debugger.Break(); but it just wouldn't load the symbols.

    I managed to solve the issue by changing automatically determine the type of code to debug to debug "Managed (v4.0) code" in the Attach to Process screen.

  • A small enhancement to the example above is to use an if statement to see if there is already a debugger attached and not break if there is... This saves you from hitting the same breakpoint every time you refresh the page after the debugger is attached.

    if (!System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();

  • I do like that. I used to use this with the earlier 6.1 version of the .NET API, which had no built in debugging support.