In CRM customizations Depth is a very important property provided for context management, which most of the time CRM developers pay less attention to.
“Depth” property(Int32 ) is hold by the IPluginExecutionContext which holds contextual information passed into a plugin. It gets the current depth of execution in the call stack and used by the platform for infinite loop prevention.
“Depth” property(Int32 ) is hold by the IPluginExecutionContext which holds contextual information passed into a plugin. It gets the current depth of execution in the call stack and used by the platform for infinite loop prevention.
How Microsoft explains the behavior of the Depth:
“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.”
In simple words:
“Depth is the
number of times a plugin/custom workflow has been called in one transaction”
See following 3 examples.
- If Contact updates the account it self in post update, it makes an infinite loop and the first time the depth is 1, then in the second time the depth is 2 and so on…
- If Contact is updated form an account update plugin, and it triggers a plugin in contact entity, then the first plugin(in account) depth is 1 and the second plugin(in contact) depth is 2
- If a workflow(WF1) triggers in contact update, and the workflow triggers another child workflow(WF2), first workflow(WF1) depth is one and the second workflow(WF2) depth is 2
The default value in depth property is 1 and the maximum
depth is 8. And, the time limit is one
hour. This means that after one hour of the first plugin/workflow execution, it
reset the depth property to 1 again.
Knowingly or unknowingly you may have experienced this when you schedule a waiting workflow recursively. If the waiting time is less than 8min your workflow files in 8th execution. Mistry behind this is this is the above reason.
But maximum depth is configurable by the Microsoft Dynamics
365 administrator using the PowerShell command Set-CrmSetting. (The
setting is WorkflowSettings.MaxDepth.)
In your plugins you can check the depth or parent context to
prevent loops.
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Checking either depth or parent context is enough, but I have used both in this example.
if (context.Depth > 1 || context.ParentContext != null)
{
return;
}
// Logic goes here.
}
But if your plugin should execute in second level (Previous
Eg2 contact update and Eg3 WF3 update), no point of checking the parent context and
need to check only the depth. The reason is the ParentContext is not null form the second level(2,3,4..).
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.Depth > 2)
{
return;
}
// Logic goes here.
}
When you use the depth property to prevent loops, use it wisely. Otherwise you might face in to trouble by missing the necessary plugin/workflow executions.
Ref : https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg327547(v=crm.8)
Ref : https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg326836(v%3Dcrm.8)
Ref : https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2015/deployment-administrators-guide/dn531194(v%3dcrm.7)
Ref : https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg328579(v=crm.8)
Ref : https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg326836(v%3Dcrm.8)
Ref : https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2015/deployment-administrators-guide/dn531194(v%3dcrm.7)
Ref : https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg328579(v=crm.8)
No comments:
Post a Comment