async Task InitAsync(ReceiverOptions options, CancellationToken receiverTerminationToken) { _logger.LogInformation($"Initializing '{nameof(BrokeredMessageReceiver<TMessage>)}' of type '{typeof(TMessage).Name}'."); options.Description ??= options.MessageReceiverPath; _infrastructureReceiver = _infrastructureProvider.GetReceiver(options.InfrastructureType); options.MessageReceiverPath = _infrastructureProvider.GetInfrastructure(options.InfrastructureType).PathBuilder.GetMessageReceivingPath(options.SendingPath, options.MessageReceiverPath); this.SendingPath = options.SendingPath; this.MessageReceiverPath = options.MessageReceiverPath; this.ErrorQueueName = options.ErrorQueuePath; this.DeadLetterQueueName = options.DeadLetterQueuePath; options.TransactionMode ??= _messageBrokerOptions.TransactionMode; _options = options; _logger.LogTrace("Initializing messaging infrastructure"); await _infrastructureReceiver.InitializeAsync(_options, receiverTerminationToken); _logger.LogTrace("Successfully initialized messaging infrastructure"); _logger.LogDebug($"Receiver options: Infrastructure type: '{_options.InfrastructureType}', Transaction Mode: '{options.TransactionMode}', Message receiver: '{options.MessageReceiverPath}', Deadletter queue: '{options.DeadLetterQueuePath}', Error queue: '{options.ErrorQueuePath}', Max receive attempts: '{options.MaxReceiveAttempts}', Message sent from: '{options.SendingPath}', Max Concurrent Receives: '{_maxConcurrentCalls}'"); _semaphore = new SemaphoreSlim(_maxConcurrentCalls, _maxConcurrentCalls); _messageReceiverLoopTokenSource = CancellationTokenSource.CreateLinkedTokenSource(receiverTerminationToken); _messageReceiverLoop = MessageReceiverLoopAsync(); this.IsReceiving = true; _logger.LogInformation($"'{nameof(BrokeredMessageReceiver<TMessage>)}' has started receiving messages of type '{typeof(TMessage).Name}'."); await _messageReceiverLoop; _logger.LogInformation($"'{nameof(BrokeredMessageReceiver<TMessage>)}' for messages of type '{typeof(TMessage).Name}' is shutting down."); }
/// <summary> /// Creates a brokered message receiver that receives messages of <typeparamref name="TMessage"/> /// </summary> /// <param name="infrastructureReceiver">The message broker infrastructure</param> /// <param name="brokeredMessageDetailProvider">Provides routing details to the brokered message receiver</param> /// <param name="messageDispatcher">Dispatches messages of <typeparamref name="TMessage"/> to the appropriate <see cref="IMessageHandler{TMessage}"/></param> /// <param name="logger">Provides logging capability</param> public BrokeredMessageReceiver(IMessagingInfrastructureReceiver <TMessage> infrastructureReceiver, IBrokeredMessageDetailProvider brokeredMessageDetailProvider, IBrokeredMessageDispatcher brokeredMessageDispatcher, ILogger <BrokeredMessageReceiver <TMessage> > logger, IServiceScopeFactory serviceFactory) { _syncLock = new object(); _infrastructureReceiver = infrastructureReceiver ?? throw new ArgumentNullException(nameof(infrastructureReceiver)); _brokeredMessageDetailProvider = brokeredMessageDetailProvider ?? throw new ArgumentNullException(nameof(brokeredMessageDetailProvider)); _brokeredMessageDispatcher = brokeredMessageDispatcher ?? throw new ArgumentNullException(nameof(brokeredMessageDispatcher)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _serviceFactory = serviceFactory ?? throw new ArgumentNullException(nameof(serviceFactory)); }
protected virtual void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { _messageReceiverLoopTokenSource?.Cancel(); _infrastructureReceiver?.Dispose(); _semaphore?.Dispose(); _messageReceiverLoopTokenSource?.Dispose(); } _infrastructureReceiver = null; _disposedValue = true; } }