/// <summary> /// Given a text expression and runtimeArgs, evaluate the expression. /// Expressions must be compatible with DataColumn.Expression. the result is cast to T /// </summary> public static object EvaluateExpression(this WfExpression expression, IRunState run) { // Evaluate try { var knownEntities = new Dictionary <string, Resource>(); using (new SecurityBypassContext()) { // we need to fetch the known entities in a bypass so that the expression // evaluation will trigger a security error rather than a missing entity. foreach (var ke in expression.WfExpressionKnownEntities) { if (ke.ReferencedEntity != null && !string.IsNullOrEmpty(ke.Name)) { if (knownEntities.ContainsKey(ke.Name)) { knownEntities[ke.Name] = ke.ReferencedEntity; } else { knownEntities.Add(ke.Name, ke.ReferencedEntity); } } } } var result = EvaluateExpressionImpl(expression, run, knownEntities); return(result); } catch (ParseException ex) { throw new WorkflowExpressionEvaluationException(ex.ShortMessage, ex.ToString(), ex); } catch (Exception ex) { var sb = new StringBuilder(); // Format runtimeArgs sb.AppendFormat("Error: {0}\n", ex.Message); sb.AppendFormat("Expression: ** {0} **\n", expression.ExpressionString ?? "null"); sb.AppendFormat("Exception: {0}\n", ex.GetType().Name); sb.AppendFormat("Expression ID: {0}\n", expression.Id); sb.AppendFormat("isTemplate: {0}\n", expression.IsTemplateString == null ? "null" : expression.IsTemplateString.ToString()); sb.AppendFormat("target: {0}\n", expression.ArgumentToPopulate != null ? expression.ArgumentToPopulate.Id.ToString() : "null"); sb.AppendFormat("parameters: \n"); run.PrettyPrint(sb); var activity = expression.ExpressionInActivity; var workflowRunId = activity != null && activity.ContainingWorkflow != null ? activity.ContainingWorkflow.Id : 0; var message = ex is PlatformSecurityException ? $"Expression failed with a security violation" : $"Expression failed during evaluation"; throw new WorkflowExpressionEvaluationException($"{message}: '{expression.ExpressionString ?? "Null"}'", sb.ToString(), ex); } }
private string GetStateString(IRunState runState) { try { var sb = new StringBuilder(); runState.PrettyPrint(sb); return(sb.ToString()); } catch (Exception ex) { EventLog.Application.WriteError(ex.ToString()); return("Unable to retrieve workflow state information"); } }