Exemplo n.º 1
0
        public EventBusRabbitMQ(IServiceScopeFactory serviceScope, IOptions <RabbitMQOptions> optionsAccessor,
                                IRabbitMQPersistentConnection persistentConnection, IEventBusManager manager, ILogger <EventBusRabbitMQ> logger, IHttpContextAccessor httpContextAccessor)
        {
            _serviceScope = serviceScope;
            _connection   = persistentConnection;
            _manager      = manager;
            _logger       = logger;

            if (optionsAccessor == null)
            {
                throw new ArgumentNullException(nameof(optionsAccessor));
            }

            if (httpContextAccessor.HttpContext != null)
            {
                var bearerToken = httpContextAccessor.HttpContext.Request
                                  .Headers["Authorization"]
                                  .FirstOrDefault(h => h.StartsWith("bearer ", StringComparison.InvariantCultureIgnoreCase));

                // Add authorization if found
                if (bearerToken != null)
                {
                    _token = bearerToken;
                    _logger.LogInformation("Found Authorization : {0}", bearerToken);
                }
            }

            _options = optionsAccessor.Value;

            if (_options.IsConsummer)
            {
                _channel = CreateConsumerChannel();
            }
        }
Exemplo n.º 2
0
        public RabbitMQPersistentConnection(ILogger <RabbitMQPersistentConnection> logger, IOptions <RabbitMQOptions> optionsAccessor)
        {
            _logger = logger;

            if (optionsAccessor == null)
            {
                throw new ArgumentNullException(nameof(optionsAccessor));
            }

            _options = optionsAccessor.Value;

            _connectionFactory = new ConnectionFactory()
            {
                HostName = _options.HostName,
                Port     = _options.Port
            };

            if (!string.IsNullOrWhiteSpace(_options.UserName))
            {
                _connectionFactory.UserName = _options.UserName;
            }

            if (!string.IsNullOrWhiteSpace(_options.Password))
            {
                _connectionFactory.Password = _options.Password;
            }

            this.PersistentConnect();
        }