Debugging Workflows and Plugins in Microsoft Dynamics CRM 4.0

by Phil Adams 30. April 2010 00:21

The concept of debugging plugins and workflows has been blogged about in the past.  There is a lot of great information out there.  One thing I have noticed about debugging is that it involves a lot of steps.  These steps take time.  You end up waiting forever from the time you make a code change to the time that you are debugging again.  I wanted to share a couple things I have learned about debugging that have saved me a great deal of time.

Time Wasters:

  • Starting and stopping IIS and the async service
  • Deploying your new assembly
  • Attaching to processes
  • Getting back to debugging after making a small code change

Let’s get started… …

Project Setup
I like to start with a vanilla system on a virtual PC (VPC) that has CRM installed.  I import the customizations into my environment and set up some quick test data.

Note:  I stay away from remote debugging when possible as it requires specific security privileges on the CRM server and you will affect users that are trying to access the system.

From my VPC I open the project that includes my workflow or plugin.  Be sure to set the build path to the bin/assembly folder of your CRM instance.  On my machine it is C:\Program Files\Microsoft Dynamics CRM\server\bin\assembly\.  It varies from installation to installation.   Building to this location allows us to register the plugin to disk and make code changes quickly without having to move files around or re-register anything.

Register Plugin
Open the registration tool and register your plugin or workflow.  Be sure to register the plugin to disk.

Note:  When you move to production I recommend registering to the database, but registering to disk works great for debugging.

Attaching to Processes
Now that you have the plugin registered, you are ready to start debugging.  You need to attach to W3WP.exe to attach to plugins since they run within IIS.  To attach to workflows you need to attach to the CRM async service which is Crmasyncservice.exe.

 
Select w3wp.exe and Crmasyncservice.exe and click “Attach.”

To expedite the process I used a trick from Janne Mattila.  You leverage VS macros to attach to the processes by using a shortcut key. I have Ctrl+Shift+V tied to a script that attaches to both w3wp and crmasyncservice.   See my script below.

Courtesy of Janne Mattila:  http://blogs.msdn.com/jannemattila/archive/2008/10/30/attaching-debugger-to-w3wp-exe-using-nice-and-easy-keyboard-shortcut.aspx

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module AttachHelper
    ' This subroutine attaches to w3wp.exe:
    Sub Attach()
        Dim attached As Boolean = False
        Dim proc As EnvDTE.Process

        For Each proc In DTE.Debugger.LocalProcesses
            If (Right(proc.Name, 8) = "w3wp.exe") Then
                proc.Attach()
                attached = True
            End If
        Next

        If attached = False Then
            MsgBox("Couldn't find w3wp.exe")
        End If

        attached = False
        For Each proc In DTE.Debugger.LocalProcesses
            If (Right(proc.Name, 19) = "CrmAsyncService.exe") Then
                proc.Attach()
                attached = True
            End If
        Next
        If attached = False Then
            MsgBox("Couldn't find crmasyncservice.exe")
        End If
    End Sub
End Module

So we are now able to quickly attach to processes.  We can debug.  Awesome!!!

 

But wait!  Next, you discover you need to make a code change.  We don’t have edit-and-continue available in plugins yet.  So, you stop the your session, make your code change and you get the following error when you try to build.

 

This is because you are trying to build to a file location that is already locked by the async service and/or IIS.  To get around this I have a bat file on my desktop and I run it every time I want to rebuild.  It includes the following:
iisreset
net stop MSCRMAsyncService
net start MSCRMAsyncService
"C:\Program Files\Internet Explorer\iexplore.exe" http://andrewvpc:5555

This script restarts IIS to unlock w3wp.exe.  Second, it restarts the async service.  Lastly, I reopen the CRM website in IE, so IIS will re-spawn itself (replace andrewvpc with your servername).  Otherwise, when you try to attach to processes it won’t be able to find w3wp.exe.

 

Now that you have ran the script, try to rebuild.  You will be rid of your error message and it will build successfully.  Now re-run your macro using Ctrl+Shift+V.  You are back to debugging again. Enjoy!!!

Source: Andrew Zimmer

Tags: , ,

CRM | CRM Plugin | MSCRM | mscrm4

Comments (2) -

Andrew Zimmer United States
5/15/2010 7:24:51 PM #

This is a straight copy-and-paste of my blog post from a year ago.  blogs.inetium.com/.../...oft-dynamics-crm-4-0.aspx

Please give credit within your post.

Thanks,
Andrew

Reply

Phil Adams United Kingdom
5/24/2010 4:43:41 PM #

Sorry Andrew,
  I do try to link back to the source of my information.
You have full credit for this entry Smile, and a good one it is too.

Phil

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About Phil

Phil has been working with CRM since the BETA of CRM v1.0 and has seen a lot of the problems arising from installation, maintenance and Development.

Phil specializes in the ISV area of CRM; Creating Add-Ons and Plugins for various clients.

Phil currently works as a Microsoft Dynamics CRM Consultant and Developer for Cambridge Online Systems Ltd In the UK.

Phil also has several Microsoft Certifications in .NET and CRM

Month List

RecentComments

Comment RSS