コード例 #1
0
        public async Task <ReceiveResponse> SendRequestAsync(StreamingRequest request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var payload = new RequestPayload()
            {
                Verb = request.Verb,
                Path = request.Path,
            };

            if (request.Streams != null)
            {
                payload.Streams = new List <StreamDescription>();
                foreach (var contentStream in request.Streams)
                {
                    var description = GetStreamDescription(contentStream);

                    payload.Streams.Add(description);
                }
            }

            var requestId = Guid.NewGuid();

            var responseCompletionSource = new TaskCompletionSource <ReceiveResponse>();

            _pendingResponses.TryAdd(requestId, responseCompletionSource);

            // Send request
            await _sender.SendRequestAsync(requestId, payload, cancellationToken).ConfigureAwait(false);

            if (request.Streams != null)
            {
                foreach (var stream in request.Streams)
                {
                    await _sender.SendStreamAsync(stream.Id, await stream.Content.ReadAsStreamAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false);
                }
            }

            return(await responseCompletionSource.Task.DefaultTimeOutAsync().ConfigureAwait(false));
        }