Esempio n. 1
0
        public static CommandRequestOptions MapGet <TCommand>(
            this CommandRequestOptions options, string route, HttpStatusCode onSuccessStatusCode = HttpStatusCode.OK, string groupName = null, Func <TCommand, HttpContext, Task> onSuccess = null, IEnumerable <Type> extensions = null, Action <CommandRequestRegistration> onRegistration = null)
            where TCommand : Command <object>
        {
            EnsureArg.IsNotNull(options, nameof(options));
            EnsureArg.IsNotNull(options.Context, nameof(options.Context));

            options.Context.Services.AddSingleton <CommandRequestRegistration>(
                sp =>
            {
                var result = new CommandRequestRegistration <TCommand>
                {
                    Route               = route,
                    OpenApiGroupName    = groupName,
                    RequestMethod       = "get",
                    OnSuccessStatusCode = onSuccessStatusCode,
                    OnSuccess           = onSuccess,
                    ExtensionTypes      = extensions
                };

                onRegistration?.Invoke(result);
                return(result);
            });

            return(options);
        }
        public static CommandRequestOptions UseAzureStorageQueue(
            this CommandRequestOptions options, Builder <AzureStorageQueueOptionsBuilder, AzureStorageQueueOptions> optionsBuilder)
        {
            options.Context.Services.AddSingleton <IQueue <CommandRequestWrapper> >(sp =>
                                                                                    new AzureStorageQueue <CommandRequestWrapper>(optionsBuilder));

            return(options);
        }
Esempio n. 3
0
        public static CommandRequestOptions UseInMemoryQueue(
            this CommandRequestOptions options, Builder <InMemoryQueueOptionsBuilder, InMemoryQueueOptions> optionsBuilder)
        {
            options.Context.Services.AddSingleton <IQueue <CommandRequestWrapper> >(sp =>
                                                                                    new InMemoryQueue <CommandRequestWrapper>(optionsBuilder));

            return(options);
        }
        public static CommandRequestOptions UseAzureBlobStorage(
            this CommandRequestOptions options, Builder <AzureBlobFileStorageOptionsBuilder, AzureBlobFileStorageOptions> optionsBuilder)
        {
            options.Context.Services.AddSingleton(sp => new CommandRequestStore(
                                                      new FileStorageLoggingDecorator(
                                                          sp.GetRequiredService <ILoggerFactory>(),
                                                          new AzureBlobFileStorage(optionsBuilder))));

            return(options);
        }
Esempio n. 5
0
        public static CommandRequestOptions UseInMemoryStorage(
            this CommandRequestOptions options)
        {
            options.Context.Services.AddSingleton(sp => new CommandRequestStore(
                                                      new FileStorageLoggingDecorator(
                                                          sp.GetRequiredService <ILoggerFactory>(),
                                                          new InMemoryFileStorage())));

            return(options);
        }
        public static CommandRequestOptions UseAzureBlobStorage(
            this CommandRequestOptions options, string connectionString = null, string containerName = "commandrequests")
        {
            options.Context.Services.AddSingleton(sp => new CommandRequestStore(
                                                      new FileStorageLoggingDecorator(
                                                          sp.GetRequiredService <ILoggerFactory>(),
                                                          new AzureBlobFileStorage(o => o
                                                                                   .ConnectionString(connectionString.EmptyToNull() ?? options.Context.Configuration["naos:commands:azureBlobStorage:connectionString"])
                                                                                   .ContainerName($"{containerName}-{HashAlgorithm.ComputeMd5Hash(options.Context.Descriptor.Name)}")))));

            return(options);
        }
Esempio n. 7
0
 public static CommandRequestOptions MapGetQueued <TCommand>(
     this CommandRequestOptions options, string route, string groupName = null, Func <TCommand, HttpContext, Task> onSuccess = null, IEnumerable <Type> extensions = null)
     where TCommand : Command <object>
 {
     return(options.MapGet <TCommand>(
                route,
                HttpStatusCode.Accepted,
                groupName,
                onSuccess,
                extensions: new[] { typeof(QueueDispatcherCommandRequestExtension) }.InsertRange(extensions),
                onRegistration: r => r.IsQueued = true));
 }
Esempio n. 8
0
        public static CommandRequestOptions UseFolderStorage(
            this CommandRequestOptions options,
            string folder = null)
        {
            options.Context.Services.AddSingleton(sp => new CommandRequestStore(
                                                      new FileStorageLoggingDecorator(
                                                          sp.GetRequiredService <ILoggerFactory>(),
                                                          new FolderFileStorage(o => o
                                                                                .Folder(folder.EmptyToNull() ?? options.Context.Configuration["naos:commands:folderStorage:folder"].EmptyToNull() ?? Path.Combine(Path.GetTempPath(), "naos_commands/requests"))))));

            return(options);
        }
        public static CommandRequestOptions UseAzureStorageQueue(
            this CommandRequestOptions options, string connectionString = null, string name = "commandrequests")
        {
            options.Context.Services.AddSingleton <IQueue <CommandRequestWrapper> >(sp =>
                                                                                    new AzureStorageQueue <CommandRequestWrapper>(o => o
                                                                                                                                  .Mediator(sp.GetRequiredService <IMediator>())
                                                                                                                                  .LoggerFactory(sp.GetRequiredService <ILoggerFactory>())
                                                                                                                                  .ConnectionString(connectionString.EmptyToNull() ?? options.Context.Configuration["naos:commands:azureStorageQueue:connectionString"])
                                                                                                                                  .Serializer(new JsonNetSerializer(TypedJsonSerializerSettings.Create())) // needs type information in json to deserialize correctly (which is needed for mediator.send)
                                                                                                                                  .QueueName($"{name}-{HashAlgorithm.ComputeMd5Hash(options.Context.Descriptor.Name)}")));

            return(options);
        }
Esempio n. 10
0
        public static CommandRequestOptions UseInMemoryQueue(
            this CommandRequestOptions options)
        {
            options.Context.Services.AddSingleton <IQueue <CommandRequestWrapper> >(sp =>
                                                                                    new InMemoryQueue <CommandRequestWrapper>(o => o
                                                                                                                              .Mediator(sp.GetRequiredService <IMediator>())
                                                                                                                              .LoggerFactory(sp.GetRequiredService <ILoggerFactory>())
                                                                                                                              .RetryDelay(TimeSpan.FromMinutes(1))
                                                                                                                              .ProcessInterval(TimeSpan.FromMilliseconds(200))
                                                                                                                              .DequeueInterval(TimeSpan.FromMilliseconds(200))));

            return(options);
        }
        public static CommandRequestOptions UseRabbitMQQueue(
            this CommandRequestOptions options,
            string name         = "commandrequests",
            TimeSpan?expiration = null,
            int?retries         = null)
        {
            var queueName     = typeof(CommandRequestWrapper).PrettyName();
            var configuration = options.Context.Configuration.GetSection("naos:commands:rabbitMQQueue").Get <RabbitMQConfiguration>();

            if (configuration?.Enabled == true)
            {
                var connectionFactory = new ConnectionFactory
                {
                    Port     = configuration.Port == 0 ? 5672 : configuration.Port,
                    HostName = configuration.Host.IsNullOrEmpty() ? "localhost" : configuration.Host, // or 'rabbitmq' in docker-compose env
                    UserName = configuration.UserName.IsNullOrEmpty() ? "guest" : configuration.UserName,
                    Password = configuration.Password.IsNullOrEmpty() ? "guest" : configuration.Password,
                    DispatchConsumersAsync = true,
                };

                options.Context.Services.AddScoped <IQueue <CommandRequestWrapper> >(sp =>
                {
                    var provider = new RabbitMQProvider(
                        sp.GetRequiredService <ILogger <RabbitMQProvider> >(),
                        connectionFactory,
                        configuration.RetryCount,
                        $"{LogKeys.Queueing} {queueName} ({sp.GetService<Naos.Foundation.ServiceDescriptor>()?.Name})");

                    return(new RabbitMQQueue <CommandRequestWrapper>(o => o
                                                                     .Mediator(sp.GetService <IMediator>())
                                                                     .Tracer(sp.GetService <ITracer>())
                                                                     .LoggerFactory(sp.GetService <ILoggerFactory>())
                                                                     .Serializer(new JsonNetSerializer(TypedJsonSerializerSettings.Create())) // needs type information in json to deserialize correctly (which is needed for mediator.send)
                                                                     .Provider(provider)
                                                                     .QueueName($"{options.Context.Descriptor.Name}-{name}")
                                                                     .Expiration(expiration)
                                                                     .Retries(retries)));
                });
            }

            return(options);
        }