简单更新插件超时

时间:2015-05-06 21:32:55

标签: dynamics-crm-2011

我有一个插件可以更新实体上的一个字段,该字段也会触发更新。我正在尝试检查Status是否已更改为“Scheduled”并保存,然后Sharepoint中必须存在某些文档类型。来自Sharepoint的数据无需任何延迟即可检索。问题是插件需要太长时间或抛出错误: UnhandledException:System.ServiceModel.FaultException:需要在commitDetail之前启动事务:ErrorCode:-2147220911

请参阅提供的代码并帮助我......

void IPlugin.Execute(IServiceProvider serviceProvider){             IPluginExecutionContext context =(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));             if(context.MessageName!=“更新”)                 返回;

        Entity targetEntity = context.InputParameters["Target"] as Entity;
        if(targetEntity.LogicalName != "new_Smiley")
            return;

        //if(context.Depth > 1)
        //    return;

        ITracingService _TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        try {
            _TracingService.Trace("About to Retrieve Smiley entity");
            Entity _Smiley = service.Retrieve("new_Smiley", targetEntity.Id, new ColumnSet(
                            "new_applicationtonew_Smileyid",
                            "new_new_Smileyidnumber",
                            "new_status",
                            "new_ppoSmileyid"
                        ));

            var Smiley_AppId = _Smiley.GetAttributeValue<EntityReference>("new_applicationtonew_Smileyid");
            if(Smiley_AppId == null)
                return;
            var SmileyId = _Smiley.GetAttributeValue<string>("new_new_Smileyidnumber");
            var SmileyStatus = _Smiley.GetAttributeValue<OptionSetValue>("new_status").Value;
            //if(!(SmileyStatus == 649770001 || new_SmileyStatus == 649770000)) {
            //    return;
            //}
            //var PPOId = _Smiley.GetAttributeValue<EntityReference>("new_ppoSmileyid").Name;
            var PPOGuid = _Smiley.GetAttributeValue<EntityReference>("new_ppoSmileyid").Id;
            _TracingService.Trace("About to Retrieve entity");
            Entity _PPO = service.Retrieve("new_ppo", PPOGuid, new ColumnSet("new_pathwayselected"));
            var PPOPathway = _PPO.GetAttributeValue<OptionSetValue>("new_pathwayselected").Value;

            _TracingService.Trace("Retrieved Smiley entity {0}...", SmileyId);
            GetSPList spList = new GetSPList();
            var liSPDocTypes = spList.GetList(SmileyId);
            foreach(var item in liSPDocTypes) {
                _TracingService.Trace(item + " ;");
            }

            Entity PreImage = (Entity)context.PreEntityImages["PreUpdateImage"];
            //Entity PostImage = (Entity)context.PostEntityImages["PostEntityImage"];
            //if(PostImage.Contains("new_status")) {
                //throw new InvalidPluginExecutionException(PreImage.GetAttributeValue<OptionSetValue>("new_status").Value.ToString() + "_" + PostImage.GetAttributeValue<OptionSetValue>("new_status").Value.ToString());
            //}

            var liJODocTypes = new List<string>() { "Type1", "Type2", "Type3", "Type4" };
            var liScopeDocTypes = new List<string>() { "Type5", "Type6", "Type7", "Type8" };

            if(SmileyStatus == 649770001) {
                foreach(var doctype in liJODocTypes) {
                    //if(liSPDocTypes.Contains(doctype) && (PPOPathway == 100000000 || PPOPathway == 649770000 || PPOPathway == 100000001)) {
                    if(liSPDocTypes.Contains(doctype)) {
                        service.Update(_Smiley);
                    }
                    else {
                        throw new InvalidPluginExecutionException();
                    }
                }
            }

            //if(context.Depth > 1)
            //    return;
        }

        catch(InvalidPluginExecutionException ex) {
            _TracingService.Trace("{0}", ex.ToString() + "_" + ex.StackTrace + " Error by Tracing Service");
            throw new InvalidPluginExecutionException("Please upload all required documents...");
        }
        catch(FaultException<OrganizationServiceFault> ex) {
            _TracingService.Trace("The application terminated with an error.");
            _TracingService.Trace("Timestamp: {0}", ex.Detail.Timestamp);
            _TracingService.Trace("Code: {0}", ex.Detail.ErrorCode);
            _TracingService.Trace("Message: {0}", ex.Detail.Message);
            _TracingService.Trace("Inner Fault: {0}",
                null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
        }
        catch(TimeoutException ex) {
            _TracingService.Trace("The application terminated with an error.");
            _TracingService.Trace("Message: {0}", ex.Message);
            _TracingService.Trace("Stack Trace: {0}", ex.StackTrace);
            _TracingService.Trace("Inner Fault: {0}",
                null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message);
        }
        catch(Exception ex) {
            _TracingService.Trace("Update: {0}", ex.ToString() + ex.StackTrace);
        }
    }
}

在插件注册工具中,我已经注册了事件插件:预操作(CRM 2011),同步。

我可能做错了。请建议我是否必须使用PreImages或错误注册插件。 如果需要更多信息,请告诉我...... 谢谢!

1 个答案:

答案 0 :(得分:0)

在插件中,这将是有问题的代码:

catch(FaultException<OrganizationServiceFault> ex) {
        _TracingService.Trace("The application terminated with an error.");
        _TracingService.Trace("Timestamp: {0}", ex.Detail.Timestamp);
        _TracingService.Trace("Code: {0}", ex.Detail.ErrorCode);
        _TracingService.Trace("Message: {0}", ex.Detail.Message);
        _TracingService.Trace("Inner Fault: {0}",
            null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
    }

当你的代码吞下FaultException时,它会破坏交易,通常 - 我会说 - 总是会导致错误。

您需要查看正在捕获的跟踪详细信息,以查看导致初始FaultException的原因。

在我用于插件的通用错误处理代码中,我总是在捕获InvalidPluginExecutionException后抛出FaultException,因为一旦发生,事务几乎没有成功完成。