diff --git a/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs b/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs index 82e964fa..c655f236 100644 --- a/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs +++ b/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs @@ -130,39 +130,46 @@ public async Task ReadMessageAsync(PipeReader pipeReader, int maxSizeOf return await ReadMessageAsync(stream, maxSizeOfHeaders, contentType); } - public Task ReadMessageAsync(Stream stream, int maxSizeOfHeaders, string contentType) + public async Task ReadMessageAsync(Stream stream, int maxSizeOfHeaders, string contentType) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } - XmlReader reader; + Message message; - var readEncoding = SoapMessageEncoderDefaults.ContentTypeToEncoding(contentType); - - if (readEncoding == null) + using (var ms = new MemoryStream()) { - // Fallback to default or writeEncoding - readEncoding = _writeEncoding; - } + await stream.CopyToAsync(ms); + ms.Seek(0, SeekOrigin.Begin); + XmlReader reader; - var supportXmlDictionaryReader = SoapMessageEncoderDefaults.TryValidateEncoding(readEncoding, out _); + var readEncoding = SoapMessageEncoderDefaults.ContentTypeToEncoding(contentType); - if (supportXmlDictionaryReader) - { - reader = XmlDictionaryReader.CreateTextReader(stream, readEncoding, ReaderQuotas, dictionaryReader => { }); - } - else - { - var streamReaderWithEncoding = new StreamReader(stream, readEncoding); - var xmlReaderSettings = new XmlReaderSettings() { IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Prohibit, CloseInput = true }; - reader = XmlReader.Create(streamReaderWithEncoding, xmlReaderSettings); - } + if (readEncoding == null) + { + // Fallback to default or writeEncoding + readEncoding = _writeEncoding; + } + + var supportXmlDictionaryReader = SoapMessageEncoderDefaults.TryValidateEncoding(readEncoding, out _); + + if (supportXmlDictionaryReader) + { + reader = XmlDictionaryReader.CreateTextReader(ms, readEncoding, ReaderQuotas, dictionaryReader => { }); + } + else + { + var streamReaderWithEncoding = new StreamReader(ms, readEncoding); + var xmlReaderSettings = new XmlReaderSettings() { IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Prohibit, CloseInput = true }; + reader = XmlReader.Create(streamReaderWithEncoding, xmlReaderSettings); + } - Message message = Message.CreateMessage(reader, maxSizeOfHeaders, MessageVersion); + message = Message.CreateMessage(reader, maxSizeOfHeaders, MessageVersion).CreateBufferedCopy(int.MaxValue).CreateMessage(); + } - return Task.FromResult(message); + return message; } public virtual async Task WriteMessageAsync(Message message, HttpContext httpContext, PipeWriter pipeWriter, bool indentXml)