Dynamics CRM 客户端的插件调试

一直以来,Dynamics CRM插件在大家的印象中都是不便于调试的,根据官方文档(http://technet.microsoft.com/zh-cn/library/gg328574.aspx),调试插件分为两步:

  1. 注册插件
  2. 在要运行插件的 Microsoft Dynamics CRM 服务器上,将调试程序附加到进程

这步骤不仅繁琐,而且在调试出错需要修改代码,还必须在修改代码后重新注册插件,然后继续第二个步骤,这样不仅繁琐低效,而且因为调试占用进程而导致服务器无响应。这在多人共享一个CRM环境的情况下是很痛苦。

今天介绍一个全新的调试方法,这个方法有两个非常吸引人的优点:

  1. 不需要注册插件,调试过程简单
  2. 修改完代码之后可直接按F5运行,非常符合编写一般C#代码的习惯。

那么废话不多说,先看效果:

按F5运行断点到处:

image

跳过断点没发现异常,可以看到客户实体多了一条数据(这条数据可是在插件没有注册的情况下生成的哦):

image

完美运行!!!

下面介绍配置方法:

首先需要下载:Rhino.Mocks

1、插件编写完成之后,右击解决方案

image

2、选择测试项目

image

3、添加方框中的引用(三个CRM程序集、Rhino.Mocks,还有箭头所指的插件程序集)

image

4、在测试项目中,添加测试代码

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using account_plugins;

namespace PA1130101PluginTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {

            //跨域调试采用这个URL,同域用http://me-crm-01/crm即可
            string server = "Url=http://me-crm-01/crm;Domain=myron;Username=fanfan;Password=Password01!";

            var myConnection = CrmConnection.Parse(server);

            IServiceProvider serviceProvider = MockRepository.GenerateMock<IServiceProvider>();
            IPluginExecutionContext context = MockRepository.GenerateMock<IPluginExecutionContext>();
            IOrganizationServiceFactory factory = MockRepository.GenerateMock<IOrganizationServiceFactory>();
            IOrganizationService service = MockRepository.GenerateMock<IOrganizationService>();
            service = new OrganizationService(myConnection);

            ParameterCollection paramBag = new ParameterCollection();

            //模拟被触发实体的那条数据
            Entity currentent = new Entity("account");
            //客户名称为张三的数据的GUID
            currentent.Id = Guid.Parse("B3CE8D5D-2672-E311-A822-00155D016509");
            paramBag.Add("Target", currentent);

            context.Stub(x => x.InputParameters).Return(paramBag);
            serviceProvider.Stub(x => x.GetService(typeof(IPluginExecutionContext))).Return(context);
            serviceProvider.Stub(x => x.GetService(typeof(IOrganizationServiceFactory))).Return(factory);
            factory.Stub(x => x.CreateOrganizationService(null)).Return(service);

            //调用插件,将封装好的serviceProvider传进去
            Test test = new Test();
            test.Execute(serviceProvider);
        }
    }
}

5、设置测试项目为启动项目

image

至此,就可以直接在本地运行调试插件而不用官方繁琐的方法了。

更新和查询测试

最开始的演示中,测试了添加数据,在crm中,我们还需要查询和更新数据,下面我用一段代码演示。

需求:插件运行时需要将关联的主要联系人的【电子邮件】的值给到张三的电子邮件字段上。

image

image

插件代码:

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace account_plugins
{
    public class Test : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
            IOrganizationServiceFactory factory = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;
            IOrganizationService service = factory.CreateOrganizationService(null);
            Entity curEntity = context.InputParameters["Target"] as Entity;
            //createAccount(service);

            UpdateAccount(service, curEntity);
        }

        private void UpdateAccount(IOrganizationService service, Entity curEntity)
        {
            //查询当前实体所有数据,
            Entity entCur = service.Retrieve(curEntity.LogicalName, curEntity.Id, new ColumnSet(true));
            string email = string.Empty;

            if (entCur.Attributes.Contains("primarycontactid")) {
                EntityReference refContact = (EntityReference)entCur.Attributes["primarycontactid"];
                Entity contact = service.Retrieve(refContact.LogicalName, refContact.Id, new ColumnSet(true));

                if (contact.Attributes.Contains("emailaddress1")) {
                    email = contact.Attributes["emailaddress1"].ToString();
                }
            }

            if (!string.IsNullOrEmpty(email)) {
                //如果联系人的电子邮件不为空,则更新客户实体
                curEntity.Attributes["emailaddress1"] = email;
                service.Update(curEntity);
            }

        }

        /// <summary>
        /// 创建一条客户数据
        /// </summary>
        /// <param name="service"></param>
        private void createAccount(IOrganizationService service)
        {
            Entity ent = new Entity("account");
            ent.Attributes["name"] = "王五";
            ent.Attributes["telephone1"] = "1122334455";
            service.Create(ent);
        }
    }
}

运行效果:

成功查询到数据:

image

查询到了email

image

插件调试完毕

image

可以看到在张三的电子邮件已经更新成功

image

终于写完了,这篇博客从2013年写到2014年,真正的跨年博客。image

新的一年,希望有个好的开始,希望Dynamics CRM会更好,更强大。

posted @ 2014-01-01 00:38  季小鱼  阅读(2039)  评论(0编辑  收藏  举报