65.9K
CodeProject 正在变化。 阅读更多。
Home

Microsoft Dynamics CRM 4.0 插件测试和调试

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (14投票s)

2010 年 6 月 26 日

GPL3

2分钟阅读

viewsIcon

70951

downloadIcon

556

一种测试/调试 Microsoft Dynamics CRM 4.0 插件的技术

引言

本文讨论了一个有助于测试和调试 Microsoft Dynamics CRM 4.0 (MSCRM4.0) 插件的工具。该工具还可以帮助 MSCRM4.0 开发人员探索插件将接收的上下文,该上下文是开发人员从未处理过的消息,从而让开发人员了解特定实体特定消息的上下文中包含的内容。该实用程序包括一个序列化插件和一个插件测试平台。

背景

序列化起着重要作用。我们使用序列化从服务器捕获插件的上下文。序列化插件代码具有序列化逻辑,用于序列化 Microsoft Dynamics CRM 4.0 在插件执行期间提供的上下文内容。

序列化插件代码

下面给出了序列化插件的完整代码。它序列化它接收到的上下文。此插件必须注册一次。

public class SerializingPlugin : IPlugin
{
    public void Execute(IPluginExecutionContext context)
    {
        XmlSerializer formatter = new XmlSerializer
                (typeof(MyPluginContext), string.Empty);
        MyPluginContext myContext = new MyPluginContext();
        myContext.Copy(context);
        string filename = string.Format(@"C:\windows\temp\{0}-{1}", 
            context.PrimaryEntityName, context.MessageName);
        string suffix = string.Empty;
        int i = 0;
        while (File.Exists(filename + suffix))
        {
            suffix = i.ToString();
            i++;
        }
        FileStream file = File.Create(filename + suffix);
        formatter.Serialize(file, myContext);
        file.Flush();
        file.Close();
    }
}

工作原理

假设我们需要调试一个在“帐户”的“Post”(发布)“Create”(创建)上执行的插件。必须注册序列化插件,并为“帐户”实体的“Create”消息创建一个步骤。然后使用所需的值创建一个帐户。序列化插件执行并在 C:\windows\temp 中创建一个 XML 文件,其中包含它接收到的上下文。将此文件作为输入提供给 TestPlugin 应用程序。

TestPlugin 应用程序

Test Application

TestPlugin 应用程序使用 Visual Studio 调试器帮助测试/调试插件。该应用程序获取与序列化插件部署在同一 CRM 实例的 CRM 用户凭据。必须分别通过单击“LoadSearializedContext”和“LoadAssembly”按钮来提供序列化的 IPluginExecutionContext 的路径和要调试的插件 DLL 的路径。最后,在提供插件类的完全限定类名(即,带有命名空间的类名)后,单击“开始”按钮开始插件执行。

btnStart_Click 事件处理程序中的 pluginClass.Execute(myPluginContext); 行中设置断点。当调试器命中该位置时,单步进入该行。确保 .pdb 文件也存在于插件 DLL 所在的同一文件夹中。

示例序列化的 IPluginExecutionContext

<MyPluginContext xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <BusinessUnitId>bada4721-db0f-4318-8471-ed85981c94da</BusinessUnitId>
  <CallerOrigin xmlns:q1=http://schemas.microsoft.com/crm/2007/CoreTypes 
    xsi:type="q1:ApplicationOrigin" />
  <CorrelationId>0d9b8e39-7b1e-4590-a03e-535464362ebd</CorrelationId>
  <CorrelationUpdatedTime>2010-06-26T19:08:57Z</CorrelationUpdatedTime>
  <Depth>1</Depth>
  <InitiatingUserId>a6c3ac10-2fc6-41a8-b24e-3b9213cfcc56</InitiatingUserId>
  <InputParameters>
    <Properties>
      <PropertyBagEntry>
        <Name>Target</Name>
        <DynamicEntity Name="account">
          <Properties xmlns="http://schemas.microsoft.com/crm/2006/WebServices">
            <Property xsi:type="StringProperty" Name="name">
              <Value>TestAccount</Value>
            </Property>
            <Property xsi:type="LookupProperty" Name="transactioncurrencyid">
              <Value type="transactioncurrency">
                  0ad13e34-ad80-de11-a371-00155d625206</Value>
            </Property>
            <Property xsi:type="OwnerProperty" Name="ownerid">
              <Value type="systemuser">81c63db2-7bee-4483-aa8b-dc179af8ac1f</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="creditonhold">
              <Value>false</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="preferredcontactmethodcode">
              <Value name="Any">1</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="donotemail">
              <Value>false</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="donotbulkemail">
              <Value>false</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="donotphone">
              <Value>false</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="donotfax">
              <Value>false</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="donotpostalmail">
              <Value>false</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="donotsendmm">
              <Value>false</Value>
            </Property>            
            <Property xsi:type="CrmBooleanProperty" Name="donotbulkpostalmail">
              <Value>false</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="isprivate">
              <Value>false</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="shippingmethodcode">
              <Value>1</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="address2_shippingmethodcode">
              <Value>1</Value>
            </Property>           
            <Property xsi:type="PicklistProperty" Name="address2_addresstypecode">
              <Value>1</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="accountratingcode">
              <Value>1</Value>
            </Property>            
            <Property xsi:type="CrmBooleanProperty" Name="participatesinworkflow">
              <Value>false</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="territorycode">
              <Value>1</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="customersizecode">
              <Value>1</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="address2_freighttermscode">
              <Value>1</Value>
            </Property>
            <Property xsi:type="CrmBooleanProperty" Name="merged">
              <Value>false</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="accountclassificationcode">
              <Value>1</Value>
            </Property>
            <Property xsi:type="PicklistProperty" Name="businesstypecode">
              <Value>1</Value>
            </Property>
          </Properties>
        </DynamicEntity>
      </PropertyBagEntry>
      <PropertyBagEntry>
        <Name>OptionalParameters</Name>
        <ArrayOfOptionalParameter>
          <OptionalParameter xmlns:q2=
        http://schemas.microsoft.com/crm/2007/WebServices 
        xsi:type="q2:CreateDuplicatesOptionalParameter">
            <q2:Value>false</q2:Value>
          </OptionalParameter>
        </ArrayOfOptionalParameter>
      </PropertyBagEntry>
    </Properties>
  </InputParameters>
  <InvocationSource>0</InvocationSource>
  <IsExecutingInOfflineMode>false</IsExecutingInOfflineMode>
  <MessageName>Create</MessageName>
  <Mode>0</Mode>
  <OrganizationId>10f25dce-ac80-de11-a371-00155d625206</OrganizationId>
  <OrganizationName>MSCRMORG</OrganizationName>
  <OutputParameters>
    <Properties>
      <PropertyBagEntry>
        <Name>id</Name>
        <anyType xmlns:q3=http://microsoft.com/wsdl/types/ 
    xsi:type="q3:guid">4785e644-5681-df11-a9e8-00155d625206</anyType>
      </PropertyBagEntry>
    </Properties>
  </OutputParameters>
  <PostEntityImages>
    <Properties />
  </PostEntityImages>
  <PreEntityImages>
    <Properties />
  </PreEntityImages>
  <PrimaryEntityName>account</PrimaryEntityName>
  <SecondaryEntityName>none</SecondaryEntityName>
  <SharedVariables>
    <Properties>
      <PropertyBagEntry>
        <Name>DefaultsAddedFlag</Name>
        <anyType xsi:type="xsd:boolean">true</anyType>
      </PropertyBagEntry>
      <PropertyBagEntry>
        <Name>ValidationOccurredFlag</Name>
        <anyType xsi:type="xsd:boolean">true</anyType>
      </PropertyBagEntry>
    </Properties>
  </SharedVariables>
  <Stage>50</Stage>
  <UserId>81c63db2-7bee-4483-aa8b-dc179af8ac1f</UserId>
</MyPluginContext>

优点

  1. 促进 MSCRM4.0 插件的测试驱动开发
  2. 可以模拟生产服务器插件问题
  3. 生产环境故障排除和调试
  4. 开发人员无需维护 CRM 实例来开发和测试插件

关注点

我了解了接口的重要性以及接口如何隐藏实现细节并支持可扩展性。IPluginExecutionContext 是一个很好的例子。XmlSerialization 是一个很棒的概念!

历史

  • 2010 年 6 月 26 日:首次发布
  • 2010 年 6 月 28 日:添加了 MetadataService 支持
  • 2010 年 7 月 16 日:添加了 TestPlugin 应用程序的字段保存和恢复功能,以进行重复测试
  • 2010 年 7 月 23 日:添加了 TestPlugin 应用程序的字段保存和恢复功能,以进行重复测试 - 更新后的源代码
© . All rights reserved.