Dynamics 365 Customer Engagement中插件的調試

微軟動態CRM專家羅勇 ,回覆319或者20190319可方便獲取本文,同時能夠在第一間獲得我發佈的最新博文信息,follow me!個人網站是 www.luoyong.me 。app

本文主要根據官方的教程 Tutorial: Debug a plug-in 而寫,使用的環境是我本身搭建在Azure VM中的,版本爲 版本 1612 (9.0.2.3034) (DB 9.0.2.3034) (本地) 。dom

最近開始個人博文使用的Dynamics 365 Customer Engagement版本都再也不使用V8.2版本,而會使用V9.X版本。插件程序集須要引用 Microsoft.CrmSdk.CoreAssemblies 這個NuGet包的最新版本,固然針對的.NET Framework 也要變化,使用的版本是 4.6.2版本,若尚未下載,能夠去 .NET SDKs for Visual Studio 下載 .NET Framework 4.6.2 Develper Pack 安裝便可。ide

根據 Tutorial: Write and register a plug-in ,我註冊個簡單的插件,代碼很簡單,以下:工具

複製代碼

using System;using System.Security.Cryptography;using System.ServiceModel;using Microsoft.Xrm.Sdk;namespace CRM.Plugins
{    public class PreWorkOrderCreate : IPlugin
    {        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity currentEntity = (Entity)context.InputParameters["Target"];
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);                try
                {                    var rng = new RNGCryptoServiceProvider();                    byte[] data = new byte[100000];                    int seed = BitConverter.ToInt32(data, 0);                    var rand = new Random(seed);
                    currentEntity["ly_autonum"] = rand.Next(1, 100000).ToString("000000");
                }                catch (FaultException<OrganizationServiceFault> ex)
                {                    throw new InvalidPluginExecutionException("An error occurred in PreWorkOrderCreate.", ex);
                }                catch (Exception ex)
                {
                    tracingService.Trace("PreWorkOrderCreate unexpected exception: {0}", ex.Message);                    throw;
                }
            }
        }
    }
}

複製代碼

 

這裏貼兩個註冊插件的截圖:由於我本身搭建的OP V9.0環境註冊插件報錯,因此我用CRM Online來完成本博文的,報錯信息以下:此插件程序集使用了版本 4.6.2 的 .NET Framework。目前,Microsoft Dynamics 365 要求插件程序集使用版本 4.5.2 的 .NET Framework。請使用 .NET Framework 版本 4.5.2 從新生成此程序集,而後重試。visual-studio

特別說明,本地部署版本註冊插件報錯的問題在 Microsoft Dynamics 365 Server v9.0 (on-premises) Update 0.3 中解決了。測試

 

 

 而後我測試下插件代碼基本有效。網站

下面開始講述如何調試插件。首先點擊插件註冊工具上的【Install Profiler】安裝,我安裝失敗,報錯以下,原來是我這個用戶雖然有系統管理員角色,可是其【Access Mode】爲Administration,因此會缺乏不少權限。將這個用戶在Office 365 Admin Portal中授予其Dynamics 365 Customer Engagement的許可(License)稍等一下子待該用戶的【Access Mode】變成Read-Write後從新點擊插件註冊工具上的【Install Profiler】按鈕便可。ui

Microsoft.Crm.CrmSecurityException: SecLib::CheckPrivilege failed. User: 28eafdf4-363a-e911-a9ae-000d3a1ad5ea, PrivilegeName: prvCreateContractTemplate, PrivilegeId: d6cd0451-bca1-42b0-9ffd-5596a3b9a8f9, Required Depth: Basic, BusinessUnitId: 5d2366b2-3b1d-e911-a9b1-000d3a1adfba, MetadataCache Privileges Count: 3998, User Privileges Count: 555 ---&gt; Microsoft.Crm.CrmSecurityException: SecLib::CheckPrivilege failed. User: 28eafdf4-363a-e911-a9ae-000d3a1ad5ea, PrivilegeName: prvCreateContractTemplate, PrivilegeId: d6cd0451-bca1-42b0-9ffd-5596a3b9a8f9, Required Depth: Basic, BusinessUnitId: 5d2366b2-3b1d-e911-a9b1-000d3a1adfba, MetadataCache Privileges Count: 3998, User Privileges Count: 555spa

 

 

 而後選中我要調試的插件步驟,而後點擊 【Start Profiling】這個菜單項插件

 

 保持不變,直接點擊 【OK】按鈕。

 

【Start Profiling】成功的話能夠看到這個插件步驟後面多了【Profiled】,而後我要觸發這個插件的執行,我這裏就簡單,建立一條【工單】記錄就能夠。而後點擊 【Stop Profiling】按鈕中止Profile,Stop Profiling成功的話,這個插件步驟後面多了的【Profiled】字眼會消失。

 

在選中要調試的插件步驟的前提下,點擊 【Debug】按鈕。

 

在彈出的對話框中,點擊Profile 文本框旁邊的向下按鈕,會彈出【Select Profile From CRM】窗口,選擇合適的記錄(每次記錄成功的Profile都有一條記錄,因此可能會出來多條,根據Created和Type Name選擇合適的)後點擊【Select】按鈕。

 

而後選擇合適的【Assembly Location】和【Plugin】,也就是插件編譯的程序集,此時不要點擊【Start Execution】按鈕,請繼續後面的步驟。

 

而後在Visual Studio對插件的代碼設置好斷點,再點擊【Debug】 > 【Attach to Process】,在彈出的以下【Attach to Process】框中,找到名稱爲【PluginRegistration.exe】的Process (前面步驟的截圖中已經告知了這個Process 的ID爲16700,恰好對上),點擊【Attach】後。點擊前面步驟截圖中的【Start Execution】按鈕。

 

能夠看到下圖,擊中斷點,你能夠盡情調試拉。

相關文章
相關標籤/搜索