Пример #1
0
        public static QueueingOptions UseInMemoryQueue <TData>(
            this QueueingOptions options,
            Action <QueueingProviderOptions <TData> > optionsAction = null,
            int?retries    = null,
            string section = "naos:queueing:serviceBus")
            where TData : class
        {
            EnsureArg.IsNotNull(options, nameof(options));
            EnsureArg.IsNotNull(options.Context, nameof(options.Context));

            var queueName = typeof(TData).PrettyName().ToLower();

            options.Context.Services.AddSingleton <IQueue <TData> >(sp =>
            {
                return(new InMemoryQueue <TData>(o => o
                                                 .Mediator(sp.GetService <IMediator>())
                                                 .Tracer(sp.GetService <ITracer>())
                                                 .LoggerFactory(sp.GetService <ILoggerFactory>())
                                                 .QueueName(queueName)
                                                 .Retries(retries)));
            });

            optionsAction?.Invoke(
                new QueueingProviderOptions <TData>(options.Context));

            options.Context.Messages.Add($"naos services builder: queueing provider added (provider={nameof(InMemoryQueue<TData>)}, queue={queueName})");

            return(options);
        }
Пример #2
0
        public static QueueingOptions UseRabbitMQQueue <TData>(
            this QueueingOptions options,
            Action <QueueingProviderOptions <TData> > optionsAction = null,
            TimeSpan?expiration = null,
            int?retries         = null,
            string section      = "naos:queueing:rabbitMQ")
            where TData : class
        {
            EnsureArg.IsNotNull(options, nameof(options));
            EnsureArg.IsNotNull(options.Context, nameof(options.Context));

            var queueName     = typeof(TData).PrettyName();
            var configuration = options.Context.Configuration.GetSection(section).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 <TData> >(sp =>
                {
                    var provider = new RabbitMQProvider(
                        sp.GetRequiredService <ILogger <RabbitMQProvider> >(),
                        connectionFactory,
                        configuration.RetryCount,
                        $"{LogKeys.Queueing} {queueName} ({sp.GetService<Naos.Foundation.ServiceDescriptor>()?.Name})");

                    return(new RabbitMQQueue <TData>(o => o
                                                     .Mediator(sp.GetService <IMediator>())
                                                     .Tracer(sp.GetService <ITracer>())
                                                     .LoggerFactory(sp.GetService <ILoggerFactory>())
                                                     .Provider(provider)
                                                     .QueueName(queueName)
                                                     .Expiration(expiration)
                                                     .Retries(retries)));
                });

                optionsAction?.Invoke(
                    new QueueingProviderOptions <TData>(options.Context));

                options.Context.Services.AddHealthChecks()
                .AddRabbitMQ(configuration.AsConnectionString(), null, $"queueing-provider-rabbitmq-{queueName}", tags: new[] { "naos" });

                options.Context.Messages.Add($"naos services builder: queueing provider added (provider={nameof(RabbitMQQueue<TData>)}, queue={queueName})");
            }
            else
            {
                throw new NaosException("no queueing rabbitmq is enabled");
            }

            return(options);
        }
Пример #3
0
        public static QueueingOptions UseAzureStorageQueue <TData>(
            this QueueingOptions options,
            Action <QueueingProviderOptions <TData> > optionsAction = null,
            TimeSpan?expiration = null,
            int?retries         = null,
            string section      = "naos:queueing:azureStorageQueue")
            where TData : class
        {
            EnsureArg.IsNotNull(options, nameof(options));
            EnsureArg.IsNotNull(options.Context, nameof(options.Context));

            var queueName     = typeof(TData).PrettyName().ToLower();
            var configuration = options.Context.Configuration.GetSection(section).Get <QueueStorageConfiguration>();

            if (configuration?.Enabled == true)
            {
                options.Context.Services.AddScoped <IQueue <TData> >(sp =>
                {
                    return(new AzureStorageQueue <TData>(o => o
                                                         .Mediator(sp.GetService <IMediator>())
                                                         .Tracer(sp.GetService <ITracer>())
                                                         .LoggerFactory(sp.GetService <ILoggerFactory>())
                                                         .ConnectionString(configuration.ConnectionString)
                                                         .QueueName(queueName)
                                                         .Expiration(expiration)
                                                         .Retries(retries)));
                });

                optionsAction?.Invoke(
                    new QueueingProviderOptions <TData>(options.Context));

                options.Context.Services.AddHealthChecks()
                .AddAzureQueueStorage(configuration.ConnectionString, queueName, $"queueing-provider-azurequeuestorage-{queueName}");

                options.Context.Messages.Add($"naos services builder: queueing provider added (provider={nameof(AzureStorageQueue<TData>)}, queue={queueName})");
            }
            else
            {
                throw new NaosException("no messaging servicebus is enabled");
            }

            return(options);
        }