Fix NullReferenceException in CRM Plugin Registration Utility

Problem

If you encounter the below error when in Dynamics CRM while trying to profile a custom workflow plugin using the CRM Plugin Registration utility

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()

System.NullReferenceException CRM Plugin Registration Screenshot

Solution

This is a known bug that has existed in the CRM Profiler for quite some time. It assumes that your custom workflow will have an input argument, if it doesn’t then you’ll see that nasty error.

The solution is to add a fake argument to workaround the issue.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyOrg.CRM.MyPlugin
{
    public class DoStuff : CodeActivity
    {
        // Fake argument to prevent CRM Profiler from crashing
        [Input("Fake Argument for Profiler")]
        [Default("Fake Argument for Profiler")]
        public InArgument MyDummyArgument { get; set; }
        //

        protected override void Execute(CodeActivityContext activityContext)
        {
            var serviceFactory = activityContext.GetExtension();
            var workflowContext = activityContext.GetExtension();
            ...
Shane Bartholomeusz

Shane Bartholomeusz

G’day, I’m a technical solutions consultant based in Perth, Australia that specialises in the Microsoft technology stack, including .NET, Dynamics 365, Azure and general web development etc.

This blog is my place to share my thoughts, helpful solutions and just random nerdy stuff.
Shane Bartholomeusz

Latest posts by Shane Bartholomeusz (see all)

Leave a Reply