【问题标题】:CRM 2011 Online - Infinite Loop Error In Plugin - Can't Find ItCRM 2011 Online - 插件中的无限循环错误 - 找不到它
【发布时间】:2013-01-13 13:12:51
【问题描述】:

我正在使用以下在机会更新时执行的插件:

public class PreOpportunityWin : Plugin
{
    public PreOpportunityWin() : base(typeof(PreOpportunityWin))
    {
        base.RegisteredEvents.Add(
        new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update", "opportunity", new Action<LocalPluginContext>(ExecuteAutonumber)));
    }

    protected void ExecuteAutonumber(LocalPluginContext localContext)
    {
        Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)localContext.PluginExecutionContext;
        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            //Organization Service
            IOrganizationService service = localContext.OrganizationService;
            //Tracing Service
            ITracingService trace = (ITracingService)localContext.TracingService;

            Entity Target = (Entity)context.InputParameters["Target"];
            var entity = service.Retrieve(
            Target.LogicalName, Target.Id, new ColumnSet(true));
            var entityStatusCode = (OptionSetValue)entity.Attributes["statuscode"];
            if (entityStatusCode.Value == 3)
            {
                //Code to execute if opportunity won
                trace.Trace("In the execute block...");

                //Depending on the retrieved name, generate the appropriate fetch xml
                string fetchXml = null;
                fetchXml = @"<fetch mapping='logical'> 
                <entity name='my_autonumber'><all-attributes/>
                <filter type=""and"">
                <condition attribute=""my_autonumberentity"" operator=""eq"" value=""opportunity"" />
                <condition attribute=""my_name"" operator=""eq"" value=""The Autonumber Record"" />
                </filter></entity></fetch>";

                try
                {
                    //Fetch the approiate autonumber record
                    EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
                    string nextIncrementNumber = string.Empty;
                    if (result.Entities.Count == 1)
                    {
                        Entity autoNumber = result.Entities[0];

                        //Lock the autonumber enity
                        lock (autoNumber)
                        {
                            if (!autoNumber.Attributes.Contains("my_counter"))
                                throw new InvalidPluginExecutionException("my_counter must contain a value");
                            if (!autoNumber.Attributes.Contains("my_incrementunit"))
                                throw new InvalidPluginExecutionException("my_IncrementUnit must contain a value");
                            int counter = Int32.Parse(autoNumber.Attributes["my_counter"].ToString());
                            int incrementUnit = Int32.Parse(autoNumber.Attributes["my_incrementunit"].ToString());
                            string prefix = autoNumber.Attributes.Contains("my_prefix") ? autoNumber.Attributes["my_prefix"].ToString() : string.Empty;
                            string prefixSeparator = autoNumber.Attributes.Contains("my_prefixseparator") ? autoNumber.Attributes["my_prefixseparator"].ToString() : string.Empty;
                            string suffix = autoNumber.Attributes.Contains("my_suffix") ? autoNumber.Attributes["my_suffix"].ToString() : string.Empty;
                            string suffixseparator = autoNumber.Attributes.Contains("my_suffixseparator") ? autoNumber.Attributes["my_suffixseparator"].ToString() : string.Empty;
                            string numberFormatter = autoNumber.Attributes.Contains("my_numberformatter") ? autoNumber.Attributes["my_numberformatter"].ToString() : string.Empty;

                            string fieldToUpdate;
                            if (autoNumber.Attributes.Contains("my_entityautonumberfield"))
                                fieldToUpdate = autoNumber.Attributes["my_entityautonumberfield"].ToString();
                            else
                                throw new InvalidPluginExecutionException("my_entityautonumberfield should not be empty");
                            nextIncrementNumber = BuildAutoNumber(entity, prefix, prefixSeparator, suffix, suffixseparator, counter, incrementUnit, numberFormatter, service);

                            //Set project number
                            entity.Attributes["new_projectnumber"] = nextIncrementNumber;
                            autoNumber.Attributes["my_counter"] = counter + incrementUnit;
                            service.Update(autoNumber);
                      }
                  }
              }
              catch (Exception ex)
              {
                  throw new InvalidPluginExecutionException(string.Format("An error occured in Autonumber plugin: {0}", ex.ToString()));
              }
          }
      }
  }

  //This function builds the autonumber itself
  private string BuildAutoNumber(Entity entity, string prefix, string prefixSeparator, string suffix, string suffixSeparator, int counter, int incrementUnit, string numberFormatter, IOrganizationService service)
  {
      bool hasPrefix = false, hasSuffix = false;
  string returnNumber = string.Empty;

      prefix = "P";
      if (!string.IsNullOrEmpty(prefix))
      {
          hasPrefix = true;
      }
      counter = counter + incrementUnit;
      returnNumber = (hasPrefix ? prefix + prefixSeparator : "") + counter.ToString(numberFormatter) + (hasSuffix ? suffix + suffixSeparator : "");
      return returnNumber;
 }    

}

此插件在更新机会时执行,但会引发以下错误:

此工作流作业已取消,因为启动它的工作流 包括一个无限循环。更正工作流逻辑,然后重试。

我在任何地方都找不到无限循环,此外,我在创建机会时使用几乎相同的代码在不同的字段中附加另一个自动编号。这两个插件之间的唯一区别是这段代码检查了获胜状态:

Entity Target = (Entity)context.InputParameters["Target"];
var entity = service.Retrieve(Target.LogicalName, Target.Id, new ColumnSet(true));
var entityStatusCode = (OptionSetValue)entity.Attributes["statuscode"];
if (entityStatusCode.Value == 3)
  //Code to execute if opportunity won

有人可以为我详细说明这个错误吗?

【问题讨论】:

  • 我脑子里冒出来的(因为我不够勤奋地阅读代码 - 虽然我重新格式化了一个重击,但放弃了),我想知道你是否可能在无意中回击并重新触发插件的代码(更新/创建)。您是否正在做一些可能导致重新更新的事情?每次都会发生吗?您可以进行shot弹枪调试并记录所有内容以查看重复调用发生的位置。
  • “my_autonumber”上是否注册了插件?

标签: c# plugins dynamics-crm-2011


【解决方案1】:

您在某个地方有一个插件会生成一个您不会中断的重复循环,请参阅我关于my_autonumber 实体的问题。

要检查的值是IPluginExecutionContext.Depth,它告诉您循环了多少次。

之后:

Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)localContext.PluginExecutionContext;

插入这一行:

if (context.Depth &gt; 1) return;

如果插件多次执行,这将终止处理。

【讨论】:

    【解决方案2】:

    小心使用 context.Depth。

    使用 context.Depth 将使您的插件失败,您有另一个插件会在您的插件中触发此操作。

    现在深度为 2,但这是因为有另一个插件调用这个插件。

    我也面临这个问题,除了使用 Context.Depth 还有其他解决方案吗?

    【讨论】:

    猜你喜欢
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多