Debugging custom workflow assemblies

Plugin Registration tool can be used to debug not just plugin assemblies, but also custom workflow assemblies. I am not sure how well known this feature is, as I could not find any documentation about this in msdn.

Prerequisite

Use the correct version of Plugin Registration tool for your organisation. I could not use Plugin Registration tool that came along with CRM SDK 7.1.1, to debug a workflow assembly running in CRM 2015 Update 0.1 (7.0.1). I could however use Plugin Registration tool 8.0.0.7198 to debug workflow assembly running in CRMOnline 2016 (8.0.1.79). So, it seems the major version and minor version have to match to enable workflow assembly debugging.

After installing the profile, select the plugin profile node. You will now see a new button called “Profile Workflow

Workflow Profile Button

Next, choose the CRM workflow which contains the custom workflow assembly. If the workflow has multiple custom workflow assembly steps, you will see each of these in this screen. You can choose the assembly to profile. It is best to choose “Persist to Entity” option, as you won’t see the exception when it is thrown by the workflow.Profile Settings

 

Once you click OK, a clone of the workflow will be created. It will have “(Profiled)” in the end. Your original workflow will now be in “Draft” state.

Workflow Cloned

Now execute the profiled workflow manually. Confirm that the workflow has finished running.

Workflow Log

Once the workflow has completed running, you can now use the serialised profile to debug the workflow assembly. After you click the “Replay Plugin Execution” button you will see this screen, which will help you to select the correct profile record.

Debug Profile Selection

I ran the workflow manually only once and hence there is only one profile row. If the workflow ran multiple times, you will see multiple rows. After you click select, you’ll then have to choose the correct workflow assembly that matches this profile.

Start Debug

Now attach the Visual Studio Debugger to the Plugin Registration Tool. Now is also a good time to put couple of breakpoints in the workflow assembly code in Visual Studio. Once you click “Start Execution”, the control should now be transferred to Visual Studio to facilitate debugging.

Visual Studio Debug

The behaviour is exactly same as plugin debugging. You can step though the code, understand the root cause of any weird behaviour and resolve it. Once you complete the execution, you will see the trace logs in the profiler.

Debug Result

One more thing: You can use this exact same process to debug an Action which has a custom assembly. Here is how the profile looks when an action with custom workflow assembly is profiled.

Debug an action

Things to note:

  1. When a workflow step is profiled, the workflow assembly containing this activity is cloned and registered as a profiled workflow assembly. It is this assembly that is used in the workflow that has the name ending with “(Profiled)”
  2. Even though you can see the workflow being profiled, stopping the workflow profile using the Plugin Registration tool seems impossible. You can see what is being profiled.Profiled Workflow
  3. In order to stop profiling, you’ll have to
    • Unpublish and delete the workflow/action that ends with “(Profiled)
    • Delete the weirdly named assembly (all guid name) from the “Default Solution“. This will be under the “Plugin Assemblies” nodeCloned Workflow Assembly

I hope this technique will help you to debug custom workflow assemblies quite easily. Happy debugging.

EDIT (01/07/2016): Please refer Issues in debugging custom workflow assemblies where I address the issues in the comment.

Advertisements

18 thoughts on “Debugging custom workflow assemblies

  1. Hi, While choosing the “Persist to entity” radio button and click the “OK” button, I am getting the below error (online version CRM 2015)

    Unhandled Exception: System.ArgumentNullException: Value cannot be null.

    Parameter name: workflowSteps

    at PluginProfiler.Library.ProfilerManagementUtility.EnableWorkflow(CrmServiceClient service, String overrideKeyFileName, Guid workflowId, Boolean persistToEntity, String persistenceSessionKey, Boolean includeSecureInformation, Boolean isContextReplay, CustomActivityStep[] workflowSteps)

    at Microsoft.Crm.Tools.PluginRegistration.ProfilerSettingsViewModel.btnOk_Click()

  2. It think it is a bug in the old versions of Plugin Registration tool, where you can click OK without selecting the workflow step. Did you select the workflow step (see screenshot 2) before clicking the OK button? You also will see the workflow step if you use the correct plugin registration tool version applicable to your CRM version.

    • Hi, Thanks for your reply. I have used ur code and steps . I have selected steps as per ur screen shot. Online version 2015. SDK version also 2015.

      While clicking the OK, I got the above error.

      I have tried last 2 days. But no improvement.

      Kindly provide suggestions

  3. I have tried to debug my WFActivity with CRM 2015 On-premise (Database/none) and CRM 2016 Online (Sandbox) and I get the same error… Object reference not set to an instance of an object.
    I have tried with Persist entity or Exception options with the same result error.

  4. I haven’t tried this in CRM2015, as I don’t have the correct version of the Plugin Registration tool. I have tried this only in CRMOnline and it was working at the time of the post. I will check this once again in CRMOnline today, and see whether this is a bug that manifests in CRM2015 or the older version of the Plugin Registration Tool.

  5. Hi, I am having Dyanmics CRM Online 2016 instance. I tried to perform the same steps you have mentioned but getting following error while click OK:

    Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
    at PluginProfiler.Library.WorkflowXamlUtility.InstrumentStep(XmlNode activityNode, XmlNamespaceManager ns, InstrumentedStepConfiguration step, String workflowAssemblyName)
    at PluginProfiler.Library.WorkflowXamlUtility.InstrumentXaml(CrmServiceClient service, String xaml, IList`1 steps)
    at PluginProfiler.Library.ProfilerManagementUtility.EnableWorkflow(CrmServiceClient service, String overrideKeyFileName, Guid workflowId, Boolean persistToEntity, String persistenceSessionKey, Boolean includeSecureInformation, Boolean isContextReplay, CustomActivityStep[] workflowSteps)
    at Microsoft.Crm.Tools.PluginRegistration.CommonControls.ProfilerSettingsViewModel.btnOk_Click()

  6. HI..
    So, I tryied to follow this step, but when I clicked on OK button on Profile Settings I had this error:

    Unhandled Exception: System.InvalidOperationException: Unable to generate a default key to use to sign the workflow shim assembly. Create a key (using sn -k) and store it in “C:\Program Files (x86)\SDK\Tools\PluginRegistration\PluginProfiler.DefaultWorkflowAssembly.snk”.

    You know why? Someone can help me??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s