首页 > 解决方案 > 2 分钟插件超时未处理异常

问题描述

我试图通过使用 Thread.Sleep(120000) 在我的插件上模拟 2 分钟超时来生成超时,但是当出现该异常时,我的 try catch 似乎无法捕捉到它们,甚至跳过了 finally 块。

我将如何正确捕获它,以便我可以针对此错误创建案例记录?

我尝试使用不同的捕获异常无济于事。我什至尝试过分析插件,但由于错误,它也不会分析插件。

protected override void ExecuteCrmPlugin(LocalPluginContext localContext) {

        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;
        ITracingService tracer = localContext.TracingService;

        try {
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) {

                Entity entity = (Entity)context.InputParameters["Target"];

                if (context.MessageName.ToLower() != "create")
                    return;

                if (entity.LogicalName.ToLower() == Contact.EntityLogicalName) {
                    tracer.Trace("Sleep for 2 min");
                    Thread.Sleep(120000);
                }
            }
        }

        catch (System.ServiceModel.FaultException<System.TimeoutException ex) {
            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
        }
        catch (System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault ex) {
            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
        }
        catch (TimeoutException e) {
            throw new InvalidPluginExecutionException("A timeout has occurred during the execution of the plugin.", e);
        }
        catch (FaultException ex) {
            throw new InvalidPluginExecutionException("Err occurred.", ex);
        }
        catch (Exception ex) {
            tracer.Trace(ex.ToString());
            throw;
        }
        finally {
            tracer.Trace("Finally");
        }
    }

在基类中,我也有相同的 catch 块。

错误是:

Unhandled exception: 
Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]
Message: An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute):  MyPlugin.Plugins.PreCreateContact: System.TimeoutException: Couldn’t complete execution of the MyPlugin.Plugins.PreCreateContact plug-in within the 2-minute limit.Detail:

<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ActivityId>397e0f4c-2e16-43ea-9368-ea76607820a5</ActivityId>
  <ErrorCode>-2147220956</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <HelpLink i:nil="true" />
  <Message>An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute):  MyPlugin.Plugins.PreCreateContact: System.TimeoutException: Couldn’t complete execution of the MyPlugin.Plugins.PreCreateContact plug-in within the 2-minute limit.</Message>
  <Timestamp>2019-07-17T00:49:48.360749Z</Timestamp>
  <ExceptionRetriable>false</ExceptionRetriable>
  <ExceptionSource>PluginExecution</ExceptionSource>
  <InnerFault i:nil="true" />
  <OriginalException>System.TimeoutException: Couldn’t complete execution of the MyPlugin.Plugins.PreCreateContact plug-in within the 2-minute limit.
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Crm.Sandbox.SandboxAppDomainHelper.Execute(IOrganizationServiceFactory organizationServiceFactory, Dictionary`2 sandboxServices, String pluginTypeName, String pluginConfiguration, String pluginSecureConfig, IPluginExecutionContext requestContext, Boolean enablePluginStackTrace, Boolean chaosFailAppDomain, String crashOnPluginExceptionMessage)
   at Microsoft.Crm.Sandbox.SandboxAppDomainHelper.Execute(IOrganizationServiceFactory organizationServiceFactory, Dictionary`2 sandboxServices, String pluginTypeName, String pluginConfiguration, String pluginSecureConfig, IPluginExecutionContext requestContext, Boolean enablePluginStackTrace, Boolean chaosFailAppDomain, String crashOnPluginExceptionMessage)
   at Microsoft.Crm.Sandbox.SandboxWorker.&lt;&gt;c__DisplayClass3_0.&lt;Execute&gt;b__0()</OriginalException>
  <TraceText>
Entered MyPlugin.Plugins.PreCreateContact.Execute(), Correlation Id: 0c2b0dd3-d27c-46ea-a7e2-90c0729b326e, Initiating User: 61e01dfa-668a-e811-8107-123456
</TraceText>
</OrganizationServiceFault>

标签: dynamics-crmdynamics-crm-onlinedynamics-365

解决方案


我想一旦插件达到 2 分钟超时,我们处理任何事情的能力就结束了。这是有道理的——如果系统允许我们在 2 分钟超时后运行一个 catch 块,那么这个 catch 块可以再运行一分钟或五分钟。

我采取的一种允许插件或自定义工作流程过程可能需要比 2 分钟超时时间更长的方法是使用取消令牌在超时之前正常关闭。您可以在 115 秒时抛出自己的超时异常并优雅地取消,而不是捕获平台的超时异常。

我什至将执行状态保留为存储在注释中的 XML,并将插件重新触发到 7 次的无限循环限制。除此之外,我还创建了四个进程,每个进程每小时重复一次,间隔 15 分钟——即主进程之后的 15、30、45 和 60 分钟。(进程可以每小时重复一次而不会触发无限循环异常)。这些进程捕获在 7 次运行后保留其状态的任何作业,并为另外 7 次重新触发它们。使用这种方法,我的工作流完成了需要数小时的计算。

请注意,这可能被认为是对异步处理系统的滥用。我承认这有点牵强,但用例需要完全在 Dynamics 内部处理大型计算。当然,“逃避”沙盒超时的标准方法是将处理转移到外部服务(例如 Azure API、Azure Functions、Flow、Logic Apps 等)


推荐阅读