Remote debugging your SharePoint 2010 Sandbox solution

I’m a big fan of sandboxed solutions because in addition to the safety features of running outside of the IIS worker process, you also are more agile as a developer. Farm solutions deployed in the GAC reset your web application after each deployment which means you may have to wait (typically 30 seconds or more) every time you deploy.

Should you find yourself having to debug a sandbox solution remotely you can do the following to get it working.

  • First, make sure that you have the Microsoft SharePoint Foundation Sandboxed Code Service running in Central Administration:

image

Note: In a multiple server farm deployment, you will want to make sure this service is running on only 1 machine to make things simpler.

  • You will need to copy the debug symbols to the SharePoint server before you can debug. By default the PDB file will be located in your Visual Studio project’s obj directory:
    image
  • Copy the PDB file to C:\ProgramData\Microsoft\SharePoint\UCCache. Note that c:\ProgramData is a hidden folder. Within this UCCache folder you should see one or more autogenerated folders that look something like this:
    image 

From here you will need to do a search to find your code’s DLL assembly file. Also, this folder may not appear until you first hit the SharePoint page that is using the code.

  • Next, you will need to launch the Remote Debugger x64 (MSVSMON.exe) on the remote SharePoint server. By default this is located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Remote Debugger\x64 (note: you need the x64 version). Copy this whole directory to the SharePoint server and launch msvsmon.exe. If it works, you should see something like this:

image 

  • Launch Visual Studio 2010 on your developer machine. Both the developer and server machines should be in the same Active Directory domain for managed code debugging. Open your Visual Studio SharePoint project that you wish to debug.
  • Launch your web browser and hit the page with your SharePoint code on it
  • Go into the Debug menu and select Attach to Process. Type the remote server name in the Qualifier box and hit Enter (note: username/domain name will appear in this box automatically). Select the SPUCWorkerProcess.exe. If your User Code service is running under a different account, you may get a warning when you try to attach.
    image
  • Go into Modules under the Debug, Windows menu (Ctrl +D M). If all is well, you should see something like this:
    image
  • Set a breakpoint in your code and go back to your web page and hit refresh. At this point, Visual Studio should hit your breakpoint.
    image
  • Final note, when I was trying this I saw that new autogenerated folders with my code appeared in c:\programdata. If this occurs, re-copy the PDB file. You may also be able to add the PDB file to your Symbol path. However, I did not get this working on my machine (I got Symbol file does not match the module)