private async Task <IOperationResult> DoReplayAsync(
            Action <IOperationProgress> onProgress,
            Stream replayStream,
            CancellationToken token)
        {
            const int commandAmountBetweenResponds = 1024;
            long      commandAmountForNextRespond  = commandAmountBetweenResponds;

            try
            {
                var progress = new IndeterminateProgressCount
                {
                    Processed = 0
                };

                // send initial progress
                onProgress(progress);

                long commandsProgress = 0;
                await foreach (var replayProgress in ReplayTxCommandHelper.ReplayAsync(Database, replayStream))
                {
                    commandsProgress = replayProgress.CommandsProgress;
                    if (replayProgress.CommandsProgress > commandAmountForNextRespond)
                    {
                        commandAmountForNextRespond = replayProgress.CommandsProgress + commandAmountBetweenResponds;

                        progress.Processed = replayProgress.CommandsProgress;
                        onProgress(progress);
                    }

                    token.ThrowIfCancellationRequested();
                }

                return(new ReplayTxOperationResult
                {
                    ExecutedCommandsAmount = commandsProgress
                });
            }
            catch (Exception e)
            {
                //Because the request is working while the file is uploading the server needs to ignore the rest of the stream
                //and the client needs to stop sending it
                HttpContext.Response.Headers["Connection"] = "close";
                throw new InvalidOperationException("Failed to process replay transaction commands", e);
            }
        }
        private IOperationResult DoReplay(
            Action <IOperationProgress> onProgress,
            Stream replayStream,
            CancellationToken token)
        {
            const int commandAmountBetweenResponds = 1024;
            long      commandAmountForNextRespond  = commandAmountBetweenResponds;

            try
            {
                long commandsProgress = 0;
                var  stopwatch        = Stopwatch.StartNew();
                stopwatch.Start();
                foreach (var replayProgress in ReplayTxCommandHelper.Replay(Database, replayStream))
                {
                    commandsProgress = replayProgress.CommandsProgress;
                    if (replayProgress.CommandsProgress > commandAmountForNextRespond)
                    {
                        commandAmountForNextRespond = replayProgress.CommandsProgress + commandAmountBetweenResponds;
                        onProgress(new ReplayTxProgress
                        {
                            ProcessedCommand = replayProgress.CommandsProgress,
                            PassedTime       = stopwatch.Elapsed
                        });
                    }

                    token.ThrowIfCancellationRequested();
                }
                stopwatch.Stop();

                return(new ReplayTxOperationResult
                {
                    ExecutedCommandsAmount = commandsProgress,
                    PassedTime = stopwatch.Elapsed
                });
            }
            catch (Exception e)
            {
                //Because the request is working while the file is uploading the server needs to ignore the rest of the stream
                //and the client needs to stop sending it
                HttpContext.Response.Headers["Connection"] = "close";
                throw new InvalidOperationException("Failed to process replay transaction commands", e);
            }
        }