Пример #1
0
        public async Task Handle(TNotification notification, CancellationToken cancellationToken = default)
        {
            if (notification == null)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            var requestMessagePayloadStream = (await _messageSerializer.Serialize(notification, _messageMetadata.RequestMetadata))
                                              ?? throw new InvalidOperationException("Request message payload was serialized to a null value");

            var responseMessagePayloadStream = await _messageTransport.SendRequest(_uri, requestMessagePayloadStream, cancellationToken);

            var messageSerializerResult = (await _messageSerializer.Deserialize(responseMessagePayloadStream))
                                          ?? throw new InvalidOperationException("Response message payload was deserialized to a null value");

            foreach (var metaData in messageSerializerResult.MessageMetadata)
            {
                _messageMetadata.ResponseMetadata.Add(metaData);
            }

            if (messageSerializerResult.Message != null)
            {
                throw new InvalidOperationException($"Response message contained a message payload, but was not expected to");
            }
        }
Пример #2
0
        private MessagePipelineDelegate BuildHandler(
            IMessageTransport messageTransport,
            IEnumerable <IMessagePipelineHandler> messageHandlers,
            CancellationToken cancellationToken
            )
        {
            var terminalHandler = (MessagePipelineDelegate)(async message => await messageTransport.SendRequest(_uri, message, cancellationToken));

            var handle = messageHandlers
                         .Reverse()
                         .Aggregate(terminalHandler, (next, outerHandle) => async message => await outerHandle.Handle(message, next, cancellationToken));

            return(handle);
        }
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken = default)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var requestMessagePayloadStream = (await _messageSerializer.Serialize(request, _messageMetadata.RequestMetadata))
                                              ?? throw new InvalidOperationException("Request message payload was serialized to a null value");

            var responseMessagePayloadStream = await _messageTransport.SendRequest(_uri, requestMessagePayloadStream, cancellationToken);

            var messageSerializerResult = (await _messageSerializer.Deserialize(responseMessagePayloadStream))
                                          ?? throw new InvalidOperationException("Response message payload was deserialized to a null value");

            foreach (var metaData in messageSerializerResult.MessageMetadata)
            {
                _messageMetadata.ResponseMetadata.Add(metaData);
            }

#pragma warning disable CS8653 // A default expression introduces a null value for a type parameter.
            if (messageSerializerResult.Message == null)
            {
                return(default);