在开发Microsoft Dynamics CRM 4.0 (MSCRM4.0)插件时,调试是一个不可或缺的环节。本文将介绍一种工具,它不仅可以帮助MSCRM4.0开发者调试插件,还能让他们探索插件接收到的上下文信息,这对于处理不熟悉的消息类型尤其有用。该工具包括一个序列化插件和一个插件测试环境。
序列化在插件调试中扮演着重要角色。利用序列化技术来捕获服务器端插件的上下文信息。序列化插件代码包含序列化逻辑,用于在插件执行期间序列化MSCRM4.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”的“account”上执行的插件。首先需要注册序列化插件,并为“account”实体的“Create”消息创建一个步骤。然后创建一个带有期望值的账户。序列化插件执行后,会在C:\windows\temp目录下创建一个包含它接收到的上下文的XML文件。将此文件作为输入提供给TestPlugin应用程序。
TestPlugin应用程序使用Visual Studio调试器帮助测试/调试插件。该应用程序需要提供与序列化插件部署在同一CRM实例中的CRM用户凭据。通过点击LoadSerializedContext和LoadAssembly按钮,分别提供序列化的IPluginExecutionContext的路径和要调试的插件DLL的路径。最后,提供插件类的完全限定类名(即带有命名空间的类名),然后点击“Start”按钮开始插件执行。
在btnStart_Click事件处理程序中的pluginClass.Execute(myPluginContext);行设置断点。当调试器到达该位置时,步入该行。确保.pdb文件也位于与插件DLL相同的文件夹中。
以下是一个序列化的IPluginExecutionContext XML示例。
<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>