我是微軟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 按鈕。
可以看到運行到斷點處了。