コード例 #1
0
        public async Task Publish <TBusEvent>(TBusEvent busEvent) where TBusEvent : IBusEvent
        {
            var eventType = busEvent.GetType();

            _knownMessageTypeVerifier.AssertValidMessageType(eventType);

            var brokeredMessage = await _brokeredMessageFactory.Create(busEvent);

            var topicPath = _router.Route(eventType, QueueOrTopic.Topic);

            using (var scope = _dependencyResolver.CreateChildScope())
            {
                Exception exception;

                var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope, brokeredMessage);
                try
                {
                    _logger.LogDispatchAction("Publishing", topicPath, brokeredMessage);

                    var topicSender = _messagingFactory.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, brokeredMessage);

                    return;
                }
                catch (Exception exc)
                {
                    exception = exc;
                }

                foreach (var interceptor in interceptors.Reverse())
                {
                    await interceptor.OnEventPublishingError(busEvent, brokeredMessage, exception);
                }
                _logger.LogDispatchError("publishing", topicPath, brokeredMessage, exception);
            }
        }
コード例 #2
0
        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);

            var brokeredMessage = (await _brokeredMessageFactory.Create(busRequest))
                                  .WithRequestTimeout(timeout)
                                  .DestinedForTopic(topicPath)
            ;
            var expiresAfter = _clock.UtcNow.AddSafely(timeout);
            var responseCorrelationWrapper = _requestResponseCorrelator.RecordMulticastRequest <TResponse>(Guid.Parse(brokeredMessage.MessageId), expiresAfter);

            using (var scope = _dependencyResolver.CreateChildScope())
            {
                Exception exception;

                var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope, brokeredMessage);
                try
                {
                    _logger.LogDispatchAction("Sending", topicPath, brokeredMessage);

                    var sender = _messagingFactory.GetTopicSender(topicPath);
                    foreach (var interceptor in interceptors)
                    {
                        await interceptor.OnMulticastRequestSending(busRequest, brokeredMessage);
                    }
                    await sender.Send(brokeredMessage);

                    foreach (var interceptor in interceptors.Reverse())
                    {
                        await interceptor.OnMulticastRequestSent(busRequest, brokeredMessage);
                    }

                    _logger.LogDispatchAction("Sent", topicPath, brokeredMessage);

                    _logger.LogDispatchAction("Waiting for response to", topicPath, brokeredMessage);
                    var response = responseCorrelationWrapper.ReturnResponsesOpportunistically(timeout);
                    _logger.LogDispatchAction("Received response to", topicPath, brokeredMessage);

                    return(response);
                }
                catch (Exception exc)
                {
                    exception = exc;
                }

                foreach (var interceptor in interceptors.Reverse())
                {
                    await interceptor.OnMulticastRequestSendingError(busRequest, brokeredMessage, exception);
                }
                _logger.LogDispatchError("sending", topicPath, brokeredMessage, exception);

                ExceptionDispatchInfo.Capture(exception).Throw();
                return(null);
            }
        }