Tuesday, 20 March 2007

How to debug SharePoint feature receivers

Just a quick one to detail how this is done in case someone finds it useful. For those still getting into working with SharePoint features, a feature receiver is a class which contains some code you've written to execute when a feature gets activated. Or deactivated, installed or uninstalled.

The key thing to note is that it's just standard ASP.Net debugging. The process is:-

  • deploy the assembly to the runtime location, either the GAC or the site bin directory. Note that if it's the bin directory your feature will also need appropriate CAS policy to grant the code the permissions it requires.
  • deploy the .pdb file to the same location. If this is the GAC, you can do the following:-

    - map a drive to the GAC folder i.e. C:\WINDOWS\assembly but using a UNC path such as [MachineName]\C$\WINDOWS\assembly. This allows you to browse the GAC without the shell which the framework puts on the folder, thus allowing you to see the actual structure of the files on disk.
    - locate the GAC_MSIL subfolder. In here you will see a directory for each assembly currently stored in the GAC. Find the directory for your assembly, and add the .pdb file so it sits next to the dll.


  • In Visual Studio, attach the debugger to the w3wp.exe process. Note that occasionally there will be 2 of these processes (e.g. when the process is being recycled), and it's possible to attach to the wrong one. Either do an IISReset to stop them both so that only 1 spins up with the next web request, or type 'iisapp' at the command prompt to get the process IDs of the running w3wp.exe processes. You can then match the correct one to the list which appears in the 'Attach debugger' dialog.
  • Activate the feature through the web UI (Site settings > Site Collection features/Site features). The debugger will now stop on any breakpoints you set.

And remember that the assembly must be built in debug mode so that the symbols are created.

7 comments:

Sanjay Munjal said...

Very helpful post. Saved me lot of time trying to figure out the simple stuff.

dougw said...

Hi Chris,
You don't need to copy the pdb file to debug in the GAC. See this post:
You Don't Need to Copy PDB Files to Debug in the GAC!


--Doug Ware

Chris O'Brien said...

Doug,

Like your style! Don't think many people are aware of that..

Chris.

Kimoz said...

Hi.

Just a tip for debugging with the stsadm tool...

You can add a System.Diagnostics.Debugger.Launch(); to the code you would like to debug. This will let you attach your favourite debugger when your code is run through the stsadm tool.

Ron said...

This is a great post, but I for SOME reason when I attach to the w3wp.exe with VS 2008 I never hit the break points. I've copied the .pdb to the GAC folder as explained. Anybody have ideas?

Benson said...

I never hit the break points. I've copied the .pdb to the GAC folder as explained. Anybody have ideas?

I have the same issue???

Ryan said...

Nice article.

I've put together a blog post with some of the more esoteric details of how Feature Receivers work. Thinks like what triggers each event and which account it will run under - not quite as obvious as it first sounds.

http://blog.pentalogic.net/2010/06/sharepoint-feature-receivers-events-details/