Example #1
0
            [CommandHandler] // Note that ICommandHandler<RecSumCommand, long> support isn't needed
            private async Task <long> RecSum(
                RecSumCommand command,
                IServiceProvider services, // Resolved via CommandContext.Services
                ICommander commander,      // Resolved via CommandContext.Services
                CancellationToken cancellationToken)
            {
                var context = CommandContext.GetCurrent();

                Debug.Assert(services == context.Services);      // context.Services is a scoped IServiceProvider
                Debug.Assert(commander == services.Commander()); // ICommander is singleton
                Debug.Assert(services != commander.Services);    // Scoped IServiceProvider != top-level IServiceProvider
                WriteLine($"Numbers: {command.Numbers.ToDelimitedString()}");

                // Each call creates a new CommandContext
                var contextStack   = new List <CommandContext>();
                var currentContext = context;

                while (currentContext != null)
                {
                    contextStack.Add(currentContext);
                    currentContext = currentContext.OuterContext;
                }
                WriteLine($"CommandContext stack size: {contextStack.Count}");
                Debug.Assert(contextStack[^ 1] == context.OutermostContext);