コード例 #1
0
        /// <inheritdoc />
        public async Task <object> ConvertFromHttpContentAsync(Type resultType, HttpContent httpContent, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!CanConvertFromHttpContent(resultType, httpContent))
            {
                throw new NotSupportedException("CanConvertFromHttpContent resulted in false, this is not supposed to be called.");
            }
            Log.Debug().WriteLine("Retrieving the content as MemoryStream, Content-Type: {0}", httpContent.Headers.ContentType);
            var httpBehaviour = HttpBehaviour.Current;

            var memoryStream = new MemoryStream();

            using (var contentStream = await httpContent.GetContentStream().ConfigureAwait(false))
            {
                await contentStream.CopyToAsync(memoryStream, httpBehaviour.ReadBufferSize, cancellationToken).ConfigureAwait(false);
            }
            // Make sure the memory stream position is at the beginning,
            // so the processing code can read right away.
            memoryStream.Position = 0;
            return(memoryStream);
        }