我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面的微软最有价值专家(Microsoft MVP),欢迎关注我的微信公众号 MSFTDynamics365erLuoYong ,回复416或者20200614可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!
前面的文章 Dynamics 365 Customer Engagement中插件的调试 谈到了插件的调试,今天我们来讲一下自定义工作流活动的调试,主要参考官方文档:Debug Workflow Activities 。
首先根据官方文档 Create a custom workflow activity 的指引创建一个基于.NET Framework 4.6.2 的类库(class library)应用程序,并为这个程序做好签名(Signing),然后通过NuGet添加对 Microsoft.CrmSdk.Workflow 的引用,我这里用如下简单的代码:
你可能会问,为啥代码中有一个DummyArgument,我的代码并不使用这个参数,这不是多余吗?
这是为了能对code activty进行调试,如果没有输入参数,在调试的时候会选择不了这个Step,它是灰色的。
这是来自帖子 Unable to debug custom workflow because it is disabled in Plugin Registration 中Hakan Altinisik 提供的答案。
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Workflow; using System; using System.Activities; namespace CRM.Workflows { public class PostWorkOrderCreateActivity : CodeActivity { [Input("Dummy for profiler")] [Default("Dummy for profiler")] public InArgument<string> DummyArgument { get; set; } protected override void Execute(CodeActivityContext executionContext) { ITracingService tracingService = executionContext.GetExtension<ITracingService>(); tracingService.Trace($"Enter {this.GetType()} on {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { var currentEntity = context.InputParameters["Target"] as Entity; byte[] data = new byte[100000]; int seed = BitConverter.ToInt32(data, 0); var rand = new Random(seed); currentEntity["new_autonum"] = rand.Next(1, 100000).ToString("000000"); service.Update(currentEntity); tracingService.Trace($"Complete {this.GetType()} on {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); } else { throw new InvalidPluginExecutionException("InputParameters don't contain Target!"); } } catch (Exception ex) { tracingService.Trace($"{this.GetType()} encountered an unexpected exception {ex.Message} on {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); throw new InvalidPluginExecutionException(ex.Message + ex.StackTrace); } } } }
还是用插件注册工具来注册,也是先注册程序集:
一般我会将自己开发的放到一个指定WorkflowActivityGroupName下面,也会改动下FriendlyName,然后保存。
然后我配置了一个简单的工作流如下使用了这个自定义工作流活动,并测试下工作流有效。
安装好 Plug-in Profiler后,在插件注册工具 Registered Plugins & Custom Workflow Activities 列表的最后可以找到Plug-in Profiler。右击它,选择 Start Profiling Workflow 。
在弹出的对话框中选择要调试的Workflow和Steps,其他的保持不变,点击OK按钮。
然后在Plug-in Profiler就会出现一个你要调试的workflow,不过这个workflow的名字后面加了 (Profiled) 。
然后通过操作触发这个worklfow运行完成,因为worklfow是异步的,所以最好等待一会儿,也可以暂时不自动删除workflow的运行记录来看是否运行完成,还可以通过Settings > Plug-in Profiles 来看,若是产生了记录则是完成了。
然后需要unregister这个workflow,右击它,选择 Unregister 。
然后点击 Plugin Registration Tool 的 REPLAY PLUG-IN EXECUTION 菜单项。
剩下的步骤和插件调试是一样的,点击Profile 旁边的向下图标选择核实的Profile记录,然后点击Assembly Location旁边的选择图标选择workflow所在的程序集
在Visual Stuido中,做好断点,然后点击 Debug > Attach to Process...
然后找到名称为PluginRegistration.exe的进程,点击 Attach 按钮。
再回到Repla Plug-in Execution 窗口,点击 Start Execution 按钮。
可以看到运行到断点处了。