MS CRM Callouts Tip - Avoid Dead Loop in Callouts铪

CRM死循环调用解决
本文介绍了一种在CRM系统的PostUpdate事件处理器中避免因调用CRM Web服务更新同一实体而导致死循环的方法。通过使用辅助类标记实体更新状态,确保在特定条件下跳过后续的事件处理器调用。
Problem: If you call CRM web service to update an entity in this entity's  PostUpdate event handler, you may get a dead loop in your Callouts. This problem is not easy to find out as the way you debug the Callouts. If you get a blank page in CRM Web UI after some Callouts is triggered, check Windows Event Logs and you may find an error saying something has timeouted.

Cause: The reason is in a PostUpdate event handler, calling of CRM web service to update the entity will trigger another PostUpdate event, then this PostUpdate event handler is executed again, then dead loop...

Solution: If you really need some update to the entity in its PostUpdate event handler, e.g. update some customized fields on this entity, here is my solution:

A helper class:

class CalloutHelper
    {
        private static System.Collections.Hashtable _FlagDictionary = Hashtable.Synchronized(new Hashtable());

        public static void SetBypassCalloutFlag(string entityGUID, string userGUID)
        {
            string flagKey = GenerateFlagKey(entityGUID,userGUID);

            if(!_FlagDictionary.ContainsKey(flagKey))
            {
                lock (_FlagDictionary.Keys.SyncRoot)
                {
                    _FlagDictionary.Add(flagKey, null);
                }
            }
        }

        public static void ResetBypassCalloutFlag(string entityGUID, string userGUID)
        {
            string flagKey = GenerateFlagKey(entityGUID, userGUID);

            if (_FlagDictionary.ContainsKey(flagKey))
            {
                lock (_FlagDictionary.Keys.SyncRoot)
                {
                    _FlagDictionary.Remove(flagKey);
                }
            }           
        }

        public static bool IsBypassCallout(string entityGUID, string userGUID)
        {
            string flagKey = GenerateFlagKey(entityGUID, userGUID);
            return _FlagDictionary.ContainsKey(flagKey);
        }

        private static string GenerateFlagKey(string entityGUID, string userGUID)
        {
            return entityGUID + "&" + userGUID;
        }
    }

At the very beginning of  PostUpdate event handler, use the helper class to check if this event is bypassed

  public override void PostUpdate(CalloutUserContext userContext, CalloutEntityContext entityContext,
            string preImageEntityXml, string postImageEntityXml)
        {

            if (CalloutHelper.IsBypassCallout(entityContext.InstanceId.ToString(), userContext.UserId.ToString()))
            {
                CalloutHelper.ResetBypassCalloutFlag(entityContext.InstanceId.ToString(), userContext.UserId.ToString());//reset flag
                return;//skip this callouts as it is flaged to be bypassed
            }

             // your PostUpdate logic goes there

             // code to call CRM web service to update this entity

CalloutHelper.SetBypassCalloutFlag(
entityContext.InstanceId.ToString(), userContext.UserId.ToString() );//set flag

}

You may have found that the key of the flag is based on combination of entity GUID and user GUID, this is assuming that one user cannot update the same entity twice simutaniously

[ Update 2007-10-22]: In callouts code when you want to update a CRM entity through the CRM web service, please ensure you use impersonation to call the web service. This will make sure you get correct userContext.UserId to make above code work. - see how to do impersonation http://msdn2.microsoft.com/en-us/library/aa682071.aspx


在人工智能研究的前沿,自然语言理解技术正受到广泛关注,其涵盖语音转写、跨语言转换、情绪判别及语义推断等多个分支。作为该领域的基石方法之一,基于大规模文本预先训练的语言表征模型,能够从海量语料中学习深层的语言规律,从而为各类后续应用任务提供强有力的语义表示支持。得益于硬件算力的提升与模型架构的持续优化,这类预训练模型已在多项自然语言理解评测中展现出卓越的性能。 本文重点探讨中文环境下的三项典型自然语言处理任务:TNEWS新闻主题归类、OCEMOTION情感倾向判断以及OCNLI语义推理验证。这三项任务分别对应文本分类、情感分析与逻辑推理三大核心方向,共同构成了从基础文本理解到复杂语义推演的技术链条。 TNEWS新闻主题归类任务旨在对涵盖政治、经济、科技、体育等多领域的新闻文本进行自动类别划分。该任务要求模型准确识别文本主旨并完成分类,属于典型的文本分类问题。 OCEMOTION情感分析任务则专注于从社交媒体、论坛评论等短文本中识别用户的情感极性。情感分析作为文本理解的重要维度,可为商业决策、舆情监测等提供关键依据,具有显著的应用价值。 OCNLI语义推理任务需要模型依据给定的前提语句与假设语句,判断后者是否可由前者逻辑推导得出。该任务检验模型对语句间语义关联与推理关系的理解能力,是衡量自然语言理解深度的重要标杆。 在上述任务中,数据分布的多标签与类别不均衡现象构成主要挑战。多标签指单一文本可能归属多个类别,而不均衡则表现为各类别样本数量差异悬殊。这种不平衡分布易导致模型过度拟合多数类别,从而削弱其泛化性能。为应对该问题,本方案综合采用了数据重采样、损失函数加权调整等技术,以提升模型在少数类别上的识别效果。 深度学习方法是实现上述任务的核心技术路径。通过设计多层神经网络结构,模型能够自动提取文本的深层特征,并建立从原始输入到任务目标的端到端映射。本方案所涉及的技术体系包括卷积神经网络、循环神经网络、长短期记忆网络以及基于自注意力机制的Transformer架构等。 参赛者需对提供的数据集进行预处理与分析,构建高效的深度学习模型,并通过训练、验证与测试环节系统评估模型性能。借助天池平台提供的强大算力资源与预训练模型基础,参赛者可进一步优化模型设计,提升任务表现。 本次研究不仅着眼于在特定评测任务上取得优异成绩,更致力于深入探索中文自然语言处理中的实际难题,为未来智能化应用与学术研究积累方法经验与技术储备。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值