public async Task Send <TBusCommand>(TBusCommand busCommand) where TBusCommand : IBusCommand { var commandType = busCommand.GetType(); _knownMessageTypeVerifier.AssertValidMessageType(commandType); var message = await _brokeredMessageFactory.Create(busCommand); await Deliver(busCommand, commandType, message); }
public async Task Send <TBusCommand>(TBusCommand busCommand) where TBusCommand : IBusCommand { var commandType = busCommand.GetType(); _knownMessageTypeVerifier.AssertValidMessageType(commandType); var destinationPath = _router.Route(commandType, QueueOrTopic.Queue); var message = await _nimbusMessageFactory.Create(destinationPath, busCommand); await Deliver(busCommand, commandType, message); }
public async Task Publish <TBusEvent>(TBusEvent busEvent) where TBusEvent : IBusEvent { var eventType = busEvent.GetType(); _knownMessageTypeVerifier.AssertValidMessageType(eventType); var topicPath = _router.Route(eventType, QueueOrTopic.Topic, _pathFactory); var brokeredMessage = await _nimbusMessageFactory.Create(topicPath, busEvent); var sw = Stopwatch.StartNew(); using (var scope = _dependencyResolver.CreateChildScope()) { Exception exception; var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope, brokeredMessage); try { _logger.LogDispatchAction("Publishing", topicPath, sw.Elapsed); var topicSender = _transport.GetTopicSender(topicPath); foreach (var interceptor in interceptors) { await interceptor.OnEventPublishing(busEvent, brokeredMessage); } await topicSender.Send(brokeredMessage); foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnEventPublished(busEvent, brokeredMessage); } _logger.LogDispatchAction("Published", topicPath, sw.Elapsed); return; } catch (Exception exc) { exception = exc; } foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnEventPublishingError(busEvent, brokeredMessage, exception); } _logger.LogDispatchError("publishing", topicPath, sw.Elapsed, exception); if (exception != null) { throw exception; } } }
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 nimbusMessage = (await _nimbusMessageFactory.Create(queuePath, busRequest)) .WithRequestTimeout(timeout) ; DispatchLoggingContext.NimbusMessage = nimbusMessage; var expiresAfter = _clock.UtcNow.Add(timeout); var responseCorrelationWrapper = _requestResponseCorrelator.RecordRequest <TResponse>(nimbusMessage.MessageId, expiresAfter); var sw = Stopwatch.StartNew(); using (var scope = _dependencyResolver.CreateChildScope()) { Exception exception; var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope, nimbusMessage); try { _logger.LogDispatchAction("Sending", queuePath, sw.Elapsed); var sender = _transport.GetQueueSender(queuePath); foreach (var interceptor in interceptors) { await interceptor.OnRequestSending(busRequest, nimbusMessage); } await sender.Send(nimbusMessage); foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnRequestSent(busRequest, nimbusMessage); } _logger.LogDispatchAction("Sent", queuePath, sw.Elapsed); _logger.LogDispatchAction("Waiting for response to", queuePath, sw.Elapsed); var response = await responseCorrelationWrapper.WaitForResponse(timeout); _logger.LogDispatchAction("Received response to", queuePath, sw.Elapsed); return(response); } catch (Exception exc) { exception = exc; } foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnRequestSendingError(busRequest, nimbusMessage, exception); } _logger.LogDispatchError("sending", queuePath, sw.Elapsed, exception); //FIXME "sending" here is a bit misleading. The message could have been sent and the response not received. ExceptionDispatchInfo.Capture(exception).Throw(); return(default(TResponse)); } }
public async Task <IEnumerable <TResponse> > SendRequest <TRequest, TResponse>(IBusMulticastRequest <TRequest, TResponse> busRequest, TimeSpan timeout) where TRequest : IBusMulticastRequest <TRequest, TResponse> where TResponse : IBusMulticastResponse { var requestType = busRequest.GetType(); _knownMessageTypeVerifier.AssertValidMessageType(requestType); var topicPath = _router.Route(requestType, QueueOrTopic.Topic, _pathFactory); var nimbusMessage = (await _nimbusMessageFactory.Create(topicPath, busRequest)) .WithRequestTimeout(timeout) .DestinedForTopic(topicPath) ; DispatchLoggingContext.NimbusMessage = nimbusMessage; var expiresAfter = _clock.UtcNow.AddSafely(timeout); var responseCorrelationWrapper = _requestResponseCorrelator.RecordMulticastRequest <TResponse>(nimbusMessage.MessageId, expiresAfter); var sw = Stopwatch.StartNew(); using (var scope = _dependencyResolver.CreateChildScope()) { Exception exception; var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope, nimbusMessage); try { _logger.LogDispatchAction("Sending", topicPath, sw.Elapsed); var sender = _transport.GetTopicSender(topicPath); foreach (var interceptor in interceptors) { await interceptor.OnMulticastRequestSending(busRequest, nimbusMessage); } await sender.Send(nimbusMessage); foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnMulticastRequestSent(busRequest, nimbusMessage); } _logger.LogDispatchAction("Sent", topicPath, sw.Elapsed); _logger.LogDispatchAction("Waiting for responses to", topicPath, sw.Elapsed); var responsesEnumerable = responseCorrelationWrapper.ReturnResponsesOpportunistically(timeout); return(responsesEnumerable); } catch (Exception exc) { exception = exc; } foreach (var interceptor in interceptors.Reverse()) { await interceptor.OnMulticastRequestSendingError(busRequest, nimbusMessage, exception); } _logger.LogDispatchError("sending", topicPath, sw.Elapsed, exception); ExceptionDispatchInfo.Capture(exception).Throw(); return(null); } }
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)); } }