Ejemplo n.º 1
0
        public static CommandsOptions AddEndpoints(
            this CommandsOptions options,
            Action <CommandRequestOptions> optionsAction = null,
            bool addDefaultRequestCommands    = true,
            bool addQueueProcessorStartupTask = true,
            bool addSwaggerDocumentProcessor  = true)
        {
            EnsureArg.IsNotNull(options, nameof(options));
            EnsureArg.IsNotNull(options.Context, nameof(options.Context));

            if (addDefaultRequestCommands)
            {
                options.Context.Services.AddSingleton <CommandRequestRegistration>(sp =>
                                                                                   new CommandRequestRegistration <EchoCommand, EchoCommandResponse> {
                    Route = "naos/commands/echo", RequestMethod = "post"
                });
                options.Context.Services.AddSingleton <CommandRequestRegistration>(sp =>
                                                                                   new CommandRequestRegistration <EchoCommand, EchoCommandResponse> {
                    Route = "naos/commands/echo", RequestMethod = "get"
                });
                options.Context.Services.AddSingleton <CommandRequestRegistration>(sp =>
                                                                                   new CommandRequestRegistration <EchoCommand, EchoCommandResponse> {
                    Route = "naos/commands/echo/{message}", RequestMethod = "get"
                });
                options.Context.Services.AddSingleton <CommandRequestRegistration>(sp =>
                                                                                   new CommandRequestRegistration <PingCommand> {
                    Route = "naos/commands/ping", RequestMethod = "get"
                });
            }

            if (addQueueProcessorStartupTask)
            {
                options.Context.Services.AddStartupTask <CommandRequestQueueProcessor>(new TimeSpan(0, 0, 3));
            }

            if (addSwaggerDocumentProcessor)
            {
                options.Context.Services.AddSingleton <IDocumentProcessor, CommandRequestDocumentProcessor>();
            }

            optionsAction?.Invoke(new CommandRequestOptions(options.Context));

            // needed for request dispatcher extensions, so they can be used on the registrations
            options.Context.Services
            .Scan(scan => scan     // https://andrewlock.net/using-scrutor-to-automatically-register-your-services-with-the-asp-net-core-di-container/
                  .FromExecutingAssembly()
                  .FromApplicationDependencies(a => !a.FullName.StartsWithAny(new[] { "Microsoft", "System", "Scrutor", "Consul" }))
                  .AddClasses(classes => classes.AssignableTo(typeof(ICommandRequestExtension)), true));

            options.Context.Messages.Add("naos services builder: command request dispatcher added"); // TODO: list available command + routes

            return(options);
        }
Ejemplo n.º 2
0
        public static CommandsOptions AddBehavior <TBehavior>(
            this CommandsOptions options)
            where TBehavior : class, ICommandBehavior
        {
            EnsureArg.IsNotNull(options, nameof(options));
            EnsureArg.IsNotNull(options.Context, nameof(options.Context));

            options.Context.Services.AddScoped <ICommandBehavior, TBehavior>();

            options.Context.Messages.Add($"naos services builder: commands behavior added (type={typeof(TBehavior).Name})"); // TODO: list available commands/handlers

            return(options);
        }
Ejemplo n.º 3
0
        // for commands loader
        /// <summary>Removes all default assemblies and classes from Commands Options.</summary>
        /// <param name="builder">Hosted Commands Service builder.</param>
        /// <seealso cref="CommandsOptions"/>
        /// <seealso cref="CommandsHandlerAttribute"/>
        /// <seealso cref="ICommandsLoader"/>
        public static IHostedCommandsServiceBuilder RemoveDefaultHandlers(this IHostedCommandsServiceBuilder builder)
        {
            // build a default instance of options to get ones that need removing
            CommandsOptions defaultOptions = new CommandsOptions();

            return(builder.Configure(options =>
            {
                foreach (Assembly asm in defaultOptions.Assemblies)
                {
                    options.Assemblies.Remove(asm);
                }
                foreach (Type type in defaultOptions.Classes)
                {
                    options.Classes.Remove(type);
                }
            }));
        }