【问题标题】:Microsoft CRM Plugin Infinite LoopMicrosoft CRM 插件无限循环
【发布时间】:2013-08-09 00:55:30
【问题描述】:

恐怕我又问了一个 MS CRM 问题。我在更新联系人记录时执行了以下代码,但它给了我一个错误,说作业被取消,因为它包含一个无限循环。谁能告诉我为什么会这样?

// <copyright file="PostContactUpdate.cs" company="">
// Copyright (c) 2013 All Rights Reserved
// </copyright>
// <author></author>
// <date>8/7/2013 2:04:26 PM</date>
// <summary>Implements the PostContactUpdate Plugin.</summary>
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
// </auto-generated>
namespace Plugins3Test
{
    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;

    /// <summary>
    /// PostContactUpdate Plugin.
    /// Fires when the following attributes are updated:
    /// All Attributes
    /// </summary>    
    public class PostContactUpdate: Plugin
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PostContactUpdate"/> class.
        /// </summary>
        public PostContactUpdate()
            : base(typeof(PostContactUpdate))
        {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "contact", new Action<LocalPluginContext>(ExecutePostContactUpdate)));

            // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
            // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
        }

        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostContactUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            // TODO: Implement your custom Plug-in business logic.

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            IServiceProvider serviceProvider = localContext.ServiceProvider;
            ITracingService tracingService = localContext.TracingService;


            // Obtain the target entity from the input parmameters.
            //Entity contextEntity = (Entity)context.InputParameters["Target"];




                Entity targetEntity = null;
                targetEntity = (Entity)context.InputParameters["Target"];
                Guid cid = targetEntity.Id;
                ColumnSet cols = new ColumnSet("jobtitle");

                Entity contact = service.Retrieve("contact", cid, cols);
                contact.Attributes["jobtitle"] = "Sometitle";
                service.Update(contact);





        }
    }
}

【问题讨论】:

    标签: plugins dynamics-crm-2011


    【解决方案1】:

    发生这种情况是因为您的插件在更新联系人时执行,并且您的代码的最后一行再次更新了联系人,这导致再次调用插件...

    然后你就有了无限循环

    您可以使用IExecutionContext.Depth 属性防止循环

    http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iexecutioncontext.depth.aspx

    但是,如果您解释您的要求,我认为可以找到解决方案。

    【讨论】:

    • 谢谢!我可以说只在 IExecution.Depth
    【解决方案2】:

    起初if IExecutionContext.Depth &lt;= 1 似乎是个好主意,但如果你有一个不同的插件来更新联系人,它可能会咬你。您应该使用插件上下文的SharedVariables

    这样的事情应该可以工作:

    将此声明作为类级字段添加到插件类中:

    public static readonly Guid HasRunKey = new Guid("{6339dc20-01ce-4f2f-b4a1-0a1285b65bff}");
    

    并将其添加为插件的第一步:

    if(context.SharedVariables.ContainsKey[HasRunKey]){
        return;
    }else{
        context.SharedVariables.Add(HasRunKey);
        // Proceed with plugin execution
    }
    

    【讨论】:

    • 谢谢达里尔!在我之前发布的代码中,我应该将 public static readonly 行放在哪里?
    • 在此之后:public class PostContactUpdate: Plugin {PostContactUpdate 构造函数的正上方。
    【解决方案3】:

    **我经历了很多试验和错误。我不知道为什么插件上下文不起作用,但这有效,但父上下文有效。这(解决方法?)有效:) **

                if (this.Context.ParentContext != null && this.Context.ParentContext.ParentContext != null)
                {
                    var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
    
                    if (!this.Context.ParentContext.ParentContext.SharedVariables.Contains(assemblyName))
                    {
                        this.Context.ParentContext.ParentContext.SharedVariables.Add(assemblyName, true.ToString() );
                    }
                    else
                    {
                       // isRecursive = true;
                      return;
                    }
                }

    【讨论】:

      【解决方案4】:

      您的插件正在更新“jobtitle”字段,我不确定此插件是否被所有联系人更新触发,或者您在 Registerfile.crmregister 插件的定义中为其设置了一些 FilteringAttributes。通过从触发此插件的属性中排除“jobtitle”字段,您可以解决您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        相关资源
        最近更新 更多