Exemple #1
0
        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 <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);
            }
        }