Dynamics 365 CE中插件的调试

微软动态CRM专家罗勇 ,回复319或者20190319可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。

本文主要根据官方的教程 Tutorial: Debug a plug-in 而写,使用的环境是我自己搭建在Azure VM中的,版本为 版本 1612 (9.0.2.3034) (DB 9.0.2.3034) (本地) 。

最近开始我的博文使用的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 安装即可。

根据 Tutorial: Write and register a plug-in ,我注册个简单的插件,代码很简答,如下:

using System;
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 rand = new Random();
                    currentEntity["ly_autonum"] = rand.Next(1, 9999).ToString("0000");
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in PreWorkOrderCreate.", ex);
                }
                catch (Exception ex)
                {
                    tracingService.Trace("PreWorkOrderCreate: {0}", ex.ToString());
                    throw;
                }
            }
        }
    }
}

这里贴两个注册插件的截图:因为我自己搭建的OP V9.0环境注册插件报错,所以我用CRM Online来完成本博文的,报错信息如下:此插件程序集使用了版本 4.6.2 的 .NET Framework。目前,Microsoft Dynamics 365 要求插件程序集使用版本 4.5.2 的 .NET Framework。请使用 .NET Framework 版本 4.5.2 重新生成此程序集,然后重试。

 然后我测试下插件代码基本有效。

扫描二维码关注公众号,回复: 5584619 查看本文章

下面开始讲述如何调试插件。首先点击插件注册工具上的【Install Profiler】安装,我安装失败,后来在界面上导入解决方案,导入插件注册工具中的PluginProfiler.Solution.zip这个解决方案成功的。

 然后选中我要调试的插件步骤,然后点击 【Start Profiling】这个菜单项

 保持不变,直接点击 【OK】按钮。

然后我要触发这个插件的执行,我这里就简单,创建一条【工单】记录就可以可以。

猜你喜欢

转载自www.cnblogs.com/luoyong0201/p/Dynamics_365_Debug_Plugin.html