private async Task Deliver <TBusCommand>(TBusCommand busCommand, Type commandType, BrokeredMessage brokeredMessage) where TBusCommand : IBusCommand { var queuePath = _router.Route(commandType, QueueOrTopic.Queue); brokeredMessage.DestinedForQueue(queuePath); using (var scope = _dependencyResolver.CreateChildScope()) { Exception exception; var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope, brokeredMessage); try { _logger.LogDispatchAction("Sending", queuePath, brokeredMessage); var sender = _messagingFactory.GetQueueSender(queuePath); foreach (var interceptor in interceptors) { await interceptor.OnCommandSending(busCommand, brokeredMessage); } await sender.Send(brokeredMessage); foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnCommandSent(busCommand, brokeredMessage); } _logger.LogDispatchAction("Sent", queuePath, brokeredMessage); return; } catch (Exception exc) { exception = exc; } foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnCommandSendingError(busCommand, brokeredMessage, exception); } _logger.LogDispatchError("sending", queuePath, brokeredMessage, exception); } }
// ReSharper disable UnusedMember.Local private async Task Dispatch <TBusRequest, TBusResponse>(TBusRequest busRequest, BrokeredMessage brokeredMessage, Type handlerType) where TBusRequest : IBusMulticastRequest <TBusRequest, TBusResponse> where TBusResponse : IBusMulticastResponse { var replyQueueName = brokeredMessage.ReplyTo; var replyQueueClient = _messagingFactory.GetQueueSender(replyQueueName); Exception exception = null; using (var scope = _dependencyResolver.CreateChildScope()) { var handler = (IHandleMulticastRequest <TBusRequest, TBusResponse>)scope.Resolve(handlerType); var inboundInterceptors = _inboundInterceptorFactory.CreateInterceptors(scope, handler, busRequest, brokeredMessage); foreach (var interceptor in inboundInterceptors) { _logger.Debug("Executing OnRequestHandlerExecuting on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerExecuting(busRequest, brokeredMessage); _logger.Debug("Executed OnRequestHandlerExecuting on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } try { var handlerTask = handler.Handle(busRequest); var longRunningTask = handlerTask as ILongRunningTask; TBusResponse response; if (longRunningTask != null) { var wrapperTask = new LongRunningTaskWrapper <TBusResponse>(handlerTask, longRunningTask, brokeredMessage, _clock, _logger, _defaultMessageLockDuration, _taskFactory); response = await wrapperTask.AwaitCompletion(); } else { response = await handlerTask; } // ReSharper disable CompareNonConstrainedGenericWithNull if (response != null) // ReSharper restore CompareNonConstrainedGenericWithNull { var responseMessage = (await _brokeredMessageFactory.CreateSuccessfulResponse(response, brokeredMessage)) .DestinedForQueue(replyQueueName) ; var outboundInterceptors = _outboundInterceptorFactory.CreateInterceptors(scope, brokeredMessage); _logger.Debug("Sending successful response message {0} to {1} [MessageId:{2}, CorrelationId:{3}]", responseMessage.SafelyGetBodyTypeNameOrDefault(), replyQueueName, brokeredMessage.MessageId, brokeredMessage.CorrelationId); foreach (var interceptor in outboundInterceptors) { await interceptor.OnMulticastResponseSending(response, brokeredMessage); } await replyQueueClient.Send(responseMessage); foreach (var interceptor in outboundInterceptors.Reverse()) { await interceptor.OnMulticastResponseSent(response, brokeredMessage); } _logger.Info("Sent successful response message {0} to {1} [MessageId:{2}, CorrelationId:{3}]", brokeredMessage.SafelyGetBodyTypeNameOrDefault(), replyQueueName, brokeredMessage.MessageId, brokeredMessage.CorrelationId); } else { _logger.Info("Handler declined to reply. [MessageId: {0}, CorrelationId: {1}]", brokeredMessage.MessageId, brokeredMessage.CorrelationId); } } catch (Exception exc) { // Capture any exception so we can send a failed response outside the catch block exception = exc; } if (exception == null) { foreach (var interceptor in inboundInterceptors.Reverse()) { _logger.Debug("Executing OnRequestHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerSuccess(busRequest, brokeredMessage); _logger.Debug("Executed OnRequestHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } } else { foreach (var interceptor in inboundInterceptors.Reverse()) { _logger.Debug("Executing OnRequestHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerError(busRequest, brokeredMessage, exception); _logger.Debug("Executed OnRequestHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } var failedResponseMessage = await _brokeredMessageFactory.CreateFailedResponse(brokeredMessage, exception); _logger.Warn("Sending failed response message to {0} [MessageId:{1}, CorrelationId:{2}]", replyQueueName, exception.Message, brokeredMessage.MessageId, brokeredMessage.CorrelationId); await replyQueueClient.Send(failedResponseMessage); _logger.Info("Sent failed response message to {0} [MessageId:{1}, CorrelationId:{2}]", replyQueueName, brokeredMessage.MessageId, brokeredMessage.CorrelationId); } } }
public async Task <TResponse> SendRequest <TRequest, TResponse>(IBusRequest <TRequest, TResponse> busRequest, TimeSpan timeout) where TRequest : IBusRequest <TRequest, TResponse> where TResponse : IBusResponse { var requestType = busRequest.GetType(); _knownMessageTypeVerifier.AssertValidMessageType(requestType); var queuePath = _router.Route(requestType, QueueOrTopic.Queue); var brokeredMessage = (await _brokeredMessageFactory.Create(busRequest)) .WithRequestTimeout(timeout) .DestinedForQueue(queuePath) ; var expiresAfter = _clock.UtcNow.Add(timeout); var responseCorrelationWrapper = _requestResponseCorrelator.RecordRequest <TResponse>(Guid.Parse(brokeredMessage.MessageId), expiresAfter); using (var scope = _dependencyResolver.CreateChildScope()) { Exception exception; var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope, brokeredMessage); try { _logger.LogDispatchAction("Sending", queuePath, brokeredMessage); var sender = _messagingFactory.GetQueueSender(queuePath); foreach (var interceptor in interceptors) { await interceptor.OnRequestSending(busRequest, brokeredMessage); } await sender.Send(brokeredMessage); foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnRequestSent(busRequest, brokeredMessage); } _logger.LogDispatchAction("Sent", queuePath, brokeredMessage); _logger.LogDispatchAction("Waiting for response to", queuePath, brokeredMessage); var response = await responseCorrelationWrapper.WaitForResponse(timeout); _logger.LogDispatchAction("Received response to", queuePath, brokeredMessage); return(response); } catch (Exception exc) { exception = exc; } foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnRequestSendingError(busRequest, brokeredMessage, exception); } _logger.LogDispatchError("sending", queuePath, brokeredMessage, exception); ExceptionDispatchInfo.Capture(exception).Throw(); return(default(TResponse)); } }