Ejemplo n.º 1
0
        protected override void Execute(CodeActivityContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            try
            {
                CodeActivityContext = context;
                Initialize();
                Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.StartTracing, SystemTypeName, Stopwatch.ElapsedMilliseconds));
                Trace(TraceHelper.Trace(WorkflowContext));
                Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExecutionContextTracingCompleted, SystemTypeName, Stopwatch.ElapsedMilliseconds));
                Validate();
                Execute();
            }
            catch (InvalidPluginExecutionException ex)
            {
                var sb = new StringBuilder();
                sb.AppendLine(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.InvalidPluginExecution, SystemTypeName, ex.Message, ex.StackTrace));

                Action <Exception> le = null;
                le = (n) => { sb.AppendLine(n.Message); sb.AppendLine(n.StackTrace); if (n.InnerException != null)
                              {
                                  le(n.InnerException);
                              }
                };
                le(ex);

                Trace(sb.ToString());

                ExceptionJson ej = new ExceptionJson
                {
                    Details = sb.ToString(),
                    Message = ex.Message,
                    Source  = ex.Source,
                };
                Exception.Set(CodeActivityContext, ej.Message);
                ExceptionJson.Set(CodeActivityContext, SerializationHelper.SerializeJson(ej));

                if (ThrowsException.Value)
                {
                    throw;
                }
            }
            catch (FaultException <OrganizationServiceFault> ex)
            {
                var sb = new StringBuilder();
                sb.AppendLine(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.OrganizationServiceFault, SystemTypeName, ex.Message, ex.StackTrace));

                if (ex.Detail == null)
                {
                    Trace(sb.ToString());

                    ExceptionJson ej = new ExceptionJson
                    {
                        Details = sb.ToString(),
                        Message = ex.Message,
                        Source  = ex.Source,
                    };
                    Exception.Set(CodeActivityContext, ej.Message);
                    ExceptionJson.Set(CodeActivityContext, SerializationHelper.SerializeJson(ej));

                    if (ThrowsException.Value)
                    {
                        throw new InvalidPluginExecutionException(ex.Message, ex);
                    }
                }
                else
                {
                    sb.AppendLine(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExceptionErrorCode, ex.Detail.ErrorCode));
                    sb.AppendLine(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExceptionMessage, ex.Detail.Message));
                    sb.AppendLine(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExceptionTrace, ex.Detail.TraceText));
                    foreach (var errorDetail in ex.Detail.ErrorDetails)
                    {
                        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExceptionDetailKey, errorDetail.Key, errorDetail.Value));
                    }

                    Trace(sb.ToString());

                    ExceptionJson ej = new ExceptionJson
                    {
                        Details = sb.ToString(),
                        Message = ex.Detail.Message,
                        Source  = ex.Source,
                    };
                    Exception.Set(CodeActivityContext, ej.Message);
                    ExceptionJson.Set(CodeActivityContext, SerializationHelper.SerializeJson(ej));

                    if (ThrowsException.Value)
                    {
                        throw new InvalidPluginExecutionException(ex.Detail.Message, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                var sb = new StringBuilder();
                sb.AppendLine(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.UndefinedFault, SystemTypeName, ex.Message, ex.StackTrace));

                Action <Exception> le = null;
                le = (n) => { sb.AppendLine(n.Message); sb.AppendLine(n.StackTrace); if (n.InnerException != null)
                              {
                                  le(n.InnerException);
                              }
                };
                le(ex);

                Trace(sb.ToString());

                ExceptionJson ej = new ExceptionJson
                {
                    Details = sb.ToString(),
                    Message = ex.Message,
                    Source  = ex.Source,
                };
                Exception.Set(CodeActivityContext, ej.Message);
                ExceptionJson.Set(CodeActivityContext, SerializationHelper.SerializeJson(ej));

                if (ThrowsException.Value)
                {
                    throw new InvalidPluginExecutionException(ex.Message, ex);
                }
            }
            finally
            {
                if (Stopwatch != null)
                {
                    Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExecutionContextTracingCompleted, SystemTypeName, Stopwatch.Elapsed.TotalMilliseconds));
                    Stopwatch.Stop();
                    Stopwatch = null;
                }
                Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.StopTracing, SystemTypeName));
            }
        }