Skip to content

Commit

Permalink
Merge pull request #1029 from andersjonsson/memorystream-optimizations
Browse files Browse the repository at this point in the history
* removed unnessecary buffered copy
* added method to deserialize SoapDefinition from stream
* removed unnessecary StreamReader usage
  • Loading branch information
andersjonsson authored Feb 29, 2024
2 parents c05fefb + 7cd18b5 commit 85a0e12
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
9 changes: 9 additions & 0 deletions src/SoapCore/DocumentationWriter/SoapMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public static SoapDefinition DeserializeFromString(string xml)
return (SoapDefinition)xs.Deserialize(sr);
}

public static SoapDefinition DeserializeFromStream(Stream xml)
{
XmlSerializer xs = new XmlSerializer(typeof(SoapDefinition));

xs.UnknownElement += _unknownElementHandler;

return (SoapDefinition)xs.Deserialize(xml);
}

public string GenerateDocumentation()
{
#if NETCOREAPP3_1_OR_GREATER
Expand Down
7 changes: 3 additions & 4 deletions src/SoapCore/MessageEncoder/SoapMessageEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,12 @@ public async Task<Message> ReadMessageAsync(Stream stream, int maxSizeOfHeaders,
else
{
var streamReaderWithEncoding = new StreamReader(ms, readEncoding);
var xmlReaderSettings = new XmlReaderSettings() { IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Prohibit, CloseInput = true };

var xmlReaderSettings = new XmlReaderSettings() { XmlResolver = null, IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Prohibit, CloseInput = true };
reader = XmlReader.Create(streamReaderWithEncoding, xmlReaderSettings);
}

var message = Message.CreateMessage(reader, maxSizeOfHeaders, MessageVersion).CreateBufferedCopy(int.MaxValue).CreateMessage();

return message;
return Message.CreateMessage(reader, maxSizeOfHeaders, MessageVersion);
}

public virtual async Task WriteMessageAsync(Message message, HttpContext httpContext, PipeWriter pipeWriter, bool indentXml)
Expand Down
10 changes: 2 additions & 8 deletions src/SoapCore/Meta/BodyWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ public static bool TryAddSchemaTypeFromXmlSchemaProviderAttribute(this XmlDictio

memoryStream.Position = 0;

var streamReader = new StreamReader(memoryStream);
var result = streamReader.ReadToEnd();

var doc = new XmlDocument();
doc.LoadXml(result);
doc.Load(memoryStream);
doc.DocumentElement.WriteContentTo(writer);

return true;
Expand Down Expand Up @@ -130,11 +127,8 @@ public static bool TryAddSchemaTypeFromXmlSchemaProviderAttribute(this XmlDictio
schema.Write(memoryStream);
memoryStream.Position = 0;

var streamReader = new StreamReader(memoryStream);
var result = streamReader.ReadToEnd();

var doc = new XmlDocument();
doc.LoadXml(result);
doc.Load(memoryStream);
doc.DocumentElement.WriteContentTo(writer);

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/SoapCore/SoapCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>SOAP protocol middleware for ASP.NET Core</Description>
<Version>1.1.0.45</Version>
<Version>1.1.0.46</Version>
<Authors>Digital Design</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<PackageId>SoapCore</PackageId>
Expand Down
7 changes: 2 additions & 5 deletions src/SoapCore/SoapEndpointMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,7 @@ private async Task ProcessMeta(HttpContext httpContext, bool showDocumentation)
var ms = new MemoryStream();
await messageEncoder.WriteMessageAsync(responseMessage, httpContext, ms, _options.IndentWsdl);
ms.Position = 0;
using var sr = new StreamReader(ms);
var wsdl = await sr.ReadToEndAsync();

var documentation = SoapDefinition.DeserializeFromString(wsdl).GenerateDocumentation();
var documentation = SoapDefinition.DeserializeFromStream(ms).GenerateDocumentation();

await httpContext.Response.WriteAsync(documentation);

Expand Down Expand Up @@ -438,7 +435,7 @@ bool TryGetRequestValue(string key, out StringValues value)

bodyWriter.WriteBodyContents(dictionaryWriter);
dictionaryWriter.Flush();
await context.Response.WriteAsync(DefaultEncodings.UTF8.GetString(ms.ToArray()));
await ms.CopyToAsync(context.Response.Body);
}

private Func<Message, Task<Message>> MakeProcessorPipe(ISoapMessageProcessor[] soapMessageProcessors, HttpContext httpContext, Func<Message, Task<Message>> processMessageFunc)
Expand Down

0 comments on commit 85a0e12

Please sign in to comment.