public async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext orchestrationContext,
            ILogger log)
        {
            ILogger replaySafeLogger = orchestrationContext.CreateReplaySafeLogger(log);

            replaySafeLogger.LogDebug("Starting new TriggerInstancesExecutionOrchestrator instance");

            WorkflowMessageEnvelope envelope =
                orchestrationContext.GetInputWithCustomSerializationSettings <WorkflowMessageEnvelope>(
                    this.serializerSettingsProvider.Instance);

            int pageNumber = envelope.GetWorkflowInstancesPageNumber();

            replaySafeLogger.LogDebug($"Processing trigger {envelope.Trigger.Id} against instances page {pageNumber}");

            string[] instanceIds =
                await orchestrationContext.CallActivityWithCustomSerializationSettingsAsync <WorkflowMessageEnvelope, string[]>(
                    nameof(GetWorkflowInstanceIdsActivity),
                    envelope,
                    this.serializerSettingsProvider.Instance);

            replaySafeLogger.LogDebug($"Retrieved {instanceIds.Length} instance Ids");

            var tasks = new List <Task>(instanceIds.Length);

            foreach (string current in instanceIds)
            {
                envelope.SetWorkflowInstanceId(this.propertyBagFactory, current);
                tasks.Add(orchestrationContext.CallActivityWithCustomSerializationSettingsAsync(
                              nameof(ProcessTriggerActivity),
                              envelope,
                              this.serializerSettingsProvider.Instance));
            }

            await Task.WhenAll(tasks);

            replaySafeLogger.LogDebug($"Processing trigger {envelope.Trigger.Id} against instances page {pageNumber}");
        }
        public async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext orchestrationContext,
            ILogger log)
        {
            ILogger replaySafeLogger = orchestrationContext.CreateReplaySafeLogger(log);

            WorkflowMessageEnvelope envelope =
                orchestrationContext.GetInputWithCustomSerializationSettings <WorkflowMessageEnvelope>(
                    this.serializerSettingsProvider.Instance);

            try
            {
                await orchestrationContext.CallActivityAsync(
                    nameof(StartLongRunningOperationActivity),
                    (envelope.OperationId, envelope.TenantId));

                if (envelope.IsStartWorkflowRequest)
                {
                    replaySafeLogger.LogDebug(
                        $"Received new start workflow request with Id {envelope.StartWorkflowInstanceRequest.RequestId} for workflow {envelope.StartWorkflowInstanceRequest.WorkflowId}");

                    await orchestrationContext.CallActivityWithCustomSerializationSettingsAsync(
                        nameof(CreateWorkflowActivity),
                        envelope,
                        this.serializerSettingsProvider.Instance);
                }
                else
                {
                    replaySafeLogger.LogDebug($"Received new workflow trigger with Id {envelope.Trigger.Id}");

                    int count = await orchestrationContext.CallActivityWithCustomSerializationSettingsAsync <WorkflowMessageEnvelope, int>(
                        nameof(GetWorkflowInstanceCountActivity),
                        envelope,
                        this.serializerSettingsProvider.Instance);

                    int pages = (int)Math.Ceiling((decimal)count / 500);

                    replaySafeLogger.LogDebug($"Found {count} instances that match. Split into {pages} pages for fan-out.");

                    var tasks = new Task[pages];

                    for (int i = 0; i < pages; i++)
                    {
                        envelope.SetWorkflowInstancesPageNumber(this.propertyBagFactory, i);
                        tasks[i] = orchestrationContext.CallSubOrchestratorWithCustomSerializationSettingsAsync(
                            nameof(TriggerInstancesExecutionOrchestrator),
                            envelope,
                            this.serializerSettingsProvider.Instance);
                    }

                    await Task.WhenAll(tasks);

                    replaySafeLogger.LogDebug("All sub-orchestrations complete");
                }

                await orchestrationContext.CallActivityAsync(
                    nameof(CompleteLongRunningOperationActivity),
                    (envelope.OperationId, envelope.TenantId));
            }
            catch (FunctionFailedException x)
            {
                replaySafeLogger.LogError($"Error during orchestration: {x}");

                await orchestrationContext.CallActivityAsync(
                    nameof(FailLongRunningOperationActivity),
                    (envelope.OperationId, envelope.TenantId));

                throw;
            }
        }