/// <summary>
        /// Method intended to be called when starting a processing step in the middle of an orchestration. It is expected
        /// that a tracking context has been created previously, at the start of the orchestration by a call to <see
        /// cref="Process.Initiate(XLANGMessage)"/> or by a call to this method. A new processing step tracking activity
        /// is always started, and it is always linked to the process tracking activity identified in <paramref
        /// name="trackingContext"/>.
        /// </summary>
        /// <param name="trackingContext">
        ///   The tracking context that is applicable just before entering the new processing step.
        /// </param>
        /// <param name="processingStepName">
        ///   The name of the processing step entered.
        /// </param>
        /// <returns>
        /// The new <see cref="TrackingContext"/> for the processing step scope.
        /// </returns>
        public static TrackingContext Initiate(TrackingContext trackingContext, string processingStepName)
        {
            if (string.IsNullOrEmpty(processingStepName))
            {
                throw new ArgumentException("Processing step name is null or empty.", "processingStepName");
            }
            if (trackingContext.ProcessActivityId.IsNullOrEmpty())
            {
                throw new ArgumentException("trackingContext.ProcessActivityId is null or empty: process tracking has not been initiated.", "trackingContext");
            }

            var processingStep = new ProcessingStep();

            processingStep.BeginProcessingStepActivity();
            processingStep.BeginTime         = DateTime.UtcNow;
            processingStep.MachineName       = Environment.MachineName;
            processingStep.ProcessActivityID = trackingContext.ProcessActivityId;
            processingStep.Status            = TrackingStatus.Pending;
            processingStep.StepName          = processingStepName;
            // set up continuation for later processing step completion or failure
            processingStep.EnableContinuation();
            processingStep.CommitProcessingStepActivity();
            processingStep.EndProcessingStepActivity();

            // propagate the tracking context with this processing step's activity id
            var newTrackingContext = trackingContext;

            newTrackingContext.ProcessingStepActivityId = processingStep.ActivityId;
            return(newTrackingContext);
        }
        private static void Terminate(TrackingContext trackingContext, string status, string errorDescription)
        {
            if (trackingContext.ProcessingStepActivityId.IsNullOrEmpty())
            {
                throw new ArgumentException("trackingContext.ProcessingStepActivityId is null or empty: processing step tracking has not been initiated.", "trackingContext");
            }

            var processingStep = new ProcessingStep(ContinuationPrefix + trackingContext.ProcessingStepActivityId)
            {
                EndTime          = DateTime.UtcNow,
                ErrorDescription = errorDescription,
                MachineName      = Environment.MachineName,
                Status           = status
            };

            processingStep.CommitProcessingStepActivity();
            processingStep.EndProcessingStepActivity();
        }