We recently noticed custom code on Dynamics 365 (CRM 8.2) environments threw a “System.AggregateException: One or more errors occurred.” instead of the usual IPluginException we want to display to the end user.

First we did some initial tests to figure out what was happening.
Then we created a simple plugin-assembly and workflow-assembly which only does one thing.
It throws an InvalidpluginExecutionException which should display “Hello Dynamics 365 custom error message” as the exception message rather then System.AggregateException.

System.AggregateException test code

First the workflow code:

public class ExceptionOnly : CodeActivity
{
 protected override void Execute(CodeActivityContext executionContext)
 {
 throw new InvalidPluginExecutionException("Hello Dynamics 365 custom error message");
 }
} 

Second the plugin code:

public void Execute(IServiceProvider serviceProvider)
{
    throw new InvalidPluginExecutionException("Hello Dynamics 365 custom error message");
}

Last but not least the System.AggregateException message:

Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): AggregateExceptionTest.ExceptionOnly: System.AggregateException: One or more errors occurred.
at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)
at Microsoft.Crm.Workflow.Services.ProxyCustomActivity.Execute(CodeActivityContext executionContext)

Results and thoughts

After testing a number of combinations, a pattern started to show.
Only the code activity assemblies that are running in the sandbox seemed affected.
Same result for CRM online as on premise.

 

Workflow asyncWorkflow syncPluginPlugin asyncAction
SandboxNOKNOKOKOKNOK
No isolationOKOKOKOKOK

 

I guess that the 8.2 release of CRM (Dynamics 365) brings some kind of parallel.foreach functionality in to the asynchronous sandbox processing engine. Currently I haven’t found any way around it.

We can catch the aggerate exception and handle it, but that doesn’t give us the possibility to display an appropriate message to the end user.

If you did find a solution, don’t hesitate and leave a comment on how to tackle this problem.