1

恐怕我的另一个 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);





        }
    }
}
4

4 回答 4

4

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

然后你有你的无限循环

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

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

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

于 2013-08-07T15:39:47.093 回答
2

起初if IExecutionContext.Depth <= 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
}
于 2013-08-07T17:36:05.307 回答
0

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

            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;
                }
            }

于 2015-04-23T14:55:48.400 回答
0

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

于 2016-10-17T12:16:53.397 回答