Dynamics 365: 详解插件的执行深度(PluginExecutionContext.Depth)

Depth这个属性会告诉你插件它在一个调用堆栈中有多深,是第几次被调用。

我们直接用段代码对Depth值来进行说明:

1. 首先我们写一个Account实体插件并用到RetrieveMultiple事件,就是当我们每次点进Accont的一个视图的时候都会调用这个RetrieveMultiple事件,但里面只是记了一条log而已:

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PluginDepthTest
{
    public class RetrieveMutiple : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if (context.Depth == 1)
            {
                tracingService.Trace($"The plugin depth is: {context.Depth}");
            }
            else if (context.Depth > 1)
            {
                tracingService.Trace($"The plugin depth is: {context.Depth}");
            }

        }
    }
}

2. 当我们每次点进Accont的一个视图的时候都会触发这个事件,我们通过Plug-in Trace Log可以看出,在这次的事务中它只执行了一次,因为Depth为1.

3. 我们将代码进行如下修改,判断一下Depth的值,当等于1的时候,我们在代码中再调用一次RetrieveMultiple, 这个时候插件会再次被执行,并且它的Depth的值也变成2。

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PluginDepthTest
{
    public class RetrieveMutiple : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if (context.Depth == 1)
            {
                QueryExpression query = new QueryExpression("account");
                query.ColumnSet.AddColumns("name");
                EntityCollection result = service.RetrieveMultiple(query);

                tracingService.Trace($"The plugin depth is: {context.Depth}");
            }
            else if (context.Depth > 1)
            {
                tracingService.Trace($"The plugin depth is: {context.Depth}");
            }

        }
    }
}

4. 我们运行一下Account实体的试图,这个时候通过Plug-in Trace Log可以看出,这次它进来了两次,第一次Depth为1,第二次Depth为2.

 第一次:

第二次:

Notes:

  • Depth最大的作用就是为了防止插件陷入无限循环,大多数情况下,我们可以忽略此值
  • Depth的默认最大值为8,时间限制为一小时。也就是说如果Depth属性在一个小时的时间限制内增加到其最大值(8),则那么系统会认为这个事务陷入了一个无限循环,插件或Workflow的执行将被强行中止,并会报以下的错:

Message: This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help.Detail:

  • 我们可以使用以下的Powershell脚本来对Depth的默认值进行修改
$setting = Get-CrmSetting -SettingType WorkflowSettings
$setting.MaxDepth = 10
Set-CrmSetting -Setting $setting

 参考文档:

IExecutionContext.Depth Property (Microsoft.Xrm.Sdk) | Microsoft Docshttps://docs.microsoft.com/zh-cn/dotnet/api/microsoft.xrm.sdk.iexecutioncontext.depth?f1url=%3FappId%3DDev16IDEF1&l=EN-US&k=k(Microsoft.Xrm.Sdk.IExecutionContext.Depth)%3Bk(SolutionItemsProject)%3Bk(TargetFrameworkMoniker-.NETFramework%2CVersion%3Dv4.6.2)%3Bk(DevLang-csharp)&rd=true&view=dataverse-sdk-latestGet-CrmSetting (Microsoft.Crm.PowerShell) | Microsoft Docshttps://docs.microsoft.com/en-us/powershell/module/microsoft.crm.powershell/get-crmsetting?view=dynamics365ce-psWorkflowSettings.MaxDepth Property (Microsoft.Xrm.Sdk.Deployment) | Microsoft Docshttps://docs.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.deployment.workflowsettings.maxdepth?view=dataverse-sdk-latest

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Stone-hdj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值