Esempio n. 1
0
        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));
            }
        }
Esempio n. 2
0
        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));
            }
        }