创建一个dynamics 365 CRM online plugin (九) - Context.Depth
让我们来看看官方文档是怎么讲的 https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg326836(v=crm.8)
Every time a running plug-in or Workflow issues a message request to the Web services that triggers another plug-in or Workflow to execute, the Depth property of the execution context is increased. If the depth property increments to its maximum value within the configured time limit, the platform considers this behavior an infinite loop and further plug-in or Workflow execution is aborted.
The maximum depth (8) and time limit (one hour) are configurable by the Microsoft Dynamics 365 administrator using the PowerShell command Set-CrmSetting. The setting is WorkflowSettings.MaxDepth. For more information, see, “Administer the deployment using Windows PowerShell” in the Deploying and administering Microsoft Dynamics CRM.
每当一个plugin或者workflow触发了一个请求到web service 并且触发了另一个plugin 和workflow去执行, Depth这个property会增长.如果在规定的时间内增长到最大值,CRM平台会认为这是一个infinit loop, 并且未来的plugin 和 worklow执行将会失败.
depth的最大值为8, 最大时间为1小时.
让我们创建一个ContextDepth.cs 的class
然后把以下的代码复制进去
// The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parameters. Entity account = (Entity)context.InputParameters["Target"]; try { tracingService.Trace(context.Depth.ToString()); if (context.Depth > 1) { return; } // In case the user removes the value if (account.Attributes["revenue"] != null) { var revenue = ((Money)account.Attributes["revenue"]).Value; revenue += 100; account.Attributes["revenue"] = new Money(revenue); } } catch (FaultException<OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex); } catch (Exception ex) { tracingService.Trace("MyPlugin: {0}", ex.ToString()); throw; } }
让我们使用plugin registeration tool 把这个assembly register到crm中.
让我们改动 annual revenue的值
保存之后,我们发现annual revenue的值已经被更改