Skip to content

Commit

Permalink
Merge pull request #1027 from akshaybheda/improve/perfByUsingMemStream
Browse files Browse the repository at this point in the history
replaced stream with memoryStream for reading the message.
  • Loading branch information
andersjonsson authored Feb 28, 2024
2 parents bfc022e + 07ded90 commit 32f1392
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/SoapCore/MessageEncoder/SoapMessageEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,39 +130,46 @@ public async Task<Message> ReadMessageAsync(PipeReader pipeReader, int maxSizeOf
return await ReadMessageAsync(stream, maxSizeOfHeaders, contentType);
}

public Task<Message> ReadMessageAsync(Stream stream, int maxSizeOfHeaders, string contentType)
public async Task<Message> 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)
Expand Down

0 comments on commit 32f1392

Please sign in to comment.