Restricting infinite loops in MS CRM plugins

IExecutionContext.Depth Property:

Today would like to share with all of Dynamics CRM folks about the important Depth property used to prevent plugins going into infinite loop.

This is property name which tracks the Depth property of plugins in ms crm.

Gets the current depth of execution in the call stack and available in the context which is being passed to plugin.

Syntax:

int Depth { get; }

Notes:
This property is basically used by the platform for infinite loop prevention. very time a running plug-in 
or when workflow issues a message request to the web services that triggers 
another plugin 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 max-
imum depth (8) and permissible time limit (one hour) are configurable by the Microsoft 
Dynamics CRM administrator by default.

If required, this setting can be changed by a powershell command
PowerShell command -- Set-CrmSetting. 

Depth is the number of times a plugin/custom workflow has been called in 
one transaction. Depth is often used to stop infinite loops where a plugin 
updates a value which triggers the plugin to run again. When a plugin/custom 
workflow triggers another plugin/trigger then the depth property is increm-
ented by one.The depth value has a maximum value called 
WorkflowSettings.MaxDepth. 
The CRM SDK (Depth)states this is set to maximum depth of 8 within one hour
and common method to avoid a recurring plugin is to check if  a plugins 
depth > 1.
This would stop the plugins from being run if was triggered from any other
plugin. The plugin would only run if triggered from the CRM form only.


Alternate solution is to use shared variables which can pass data between plugins
registered in different stages in a transaction. Eg.You can use ShareVariables 
to pass information between plugins registered on different stages but in the 
same transaction e.g a pre plugin can pass information to a post plugin.


Details can be observed better @ use shared variables in plugins

Update: 11/26/2018

Scenario:

Think of situation where the Plugin is registered on Update of an Entity with filtering attributes set to a field in the Entity.

Due to some bad coding logic, the Plugin registered is also updating the same field.

This would call the same plugin once more causing infinite loop.

This is where using Depth becomes significant.

Thank you.

Cheers,

PMDY

Advertisement
ECELLORS CRM Blog

Sharing thoughts and challenges on Power Platform & Azure along with respective solutions put together...

SharePains by Microsoft MVP Pieter Veenstra

Microsoft 365, Power Platform, SharePoint, Teams, Azure and Dynamics

D365 Demystified

A closer look at Microsoft Dynamics 365.

Vicky Rodgers - Microsoft Dynamics 365

Everything Microsoft Dynamics 365 for Customer Engagement

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

Microsoft Dynamics 365 UK MCT

All about Microsoft Dynamics 365

High Voltages

Arduino | ESP | Raspberry pi | IoT

Mihail's space

Just another WordPress.com site

Arun Potti's MS CRM blog

Microsoft Dynamics CRM

Sander van de Velde

Microsoft MVP Azure | IoT Platform Architect | Speaker about IoT | Let me add some value

Power Platform Blog

Author : Dharanidharan Balasubramaniam

That API Guy

Making the world a better place one PowerApp/Flow at a time!

Dynamics Back Page

Tips, Tricks, and General Musings on Dynamics 365 (CRM), the Power Platform, Power Apps, Power Automate, Power BI and related technologies

Azure Integration services for Dynamics 365

Azure Integration services for Dynamics 365 Unified Operations, Logic Apps, Azure Functions, REST

Power Maverick

Incredible journey into #Dynamics365, #CDS, #PowerApps #Azure and all good stuff that #Microsoft releases

Unleashing CRM

My Technical Thoughts about Microsoft Dynamics CRM- Unleashed

Srikanth Alluri

My experiences in & around Power Platform + Azure

Debajit's Power Apps & Dynamics 365 Blog

All about Power Apps & Dynamics

Rajeev Pentyala - Microsoft Power Platform

Sharing my knowledge on Power Platform, Dynamics 365, Azure & .Net Stack

Bansal Blogs - Dynamics 365, PowerApps, Microsoft Flows, Power BI

Programming demands a lot more than simple mastery in tools and technologies

%d bloggers like this: