Skip to content

Commit 1e1b4f8

Browse files
Merge pull request #1139 from andersjonsson/fix-onwritebodycontents
Fix onwritebodycontents
2 parents 35684e7 + f2ba7f5 commit 1e1b4f8

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/SoapCore.Tests/XDocumentXmlReaderTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6+
using System.ServiceModel.Channels;
67
using System.Text;
78
using System.Threading.Tasks;
9+
using System.Xml;
810
using System.Xml.Linq;
911
using Microsoft.VisualStudio.TestTools.UnitTesting;
1012

@@ -52,5 +54,24 @@ public void TestReadBase64()
5254

5355
Assert.AreEqual(1336, ms.Length);
5456
}
57+
58+
[TestMethod]
59+
public async Task WriteTwice()
60+
{
61+
var body = $@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
62+
<soapenv:Body>
63+
<Ping xmlns=""http://tempuri.org/"">
64+
<s>1</s>
65+
</Ping>
66+
</soapenv:Body>
67+
</soapenv:Envelope>
68+
";
69+
ParsedMessage pm = await ParsedMessage.FromStreamReaderAsync(new StreamReader(new MemoryStream(Encoding.Default.GetBytes(body))), MessageVersion.Soap12);
70+
71+
var dw = XmlDictionaryWriter.CreateDictionaryWriter(XmlWriter.Create(new MemoryStream()));
72+
73+
pm.WriteBodyContents(dw);
74+
pm.WriteBodyContents(dw);
75+
}
5576
}
5677
}

src/SoapCore/ParsedMessage.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using System.Reflection;
45
using System.ServiceModel.Channels;
56
using System.Text;
67
using System.Threading;
@@ -77,13 +78,22 @@ public override string ToString()
7778

7879
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
7980
{
80-
using (var reader = GetReaderAtBodyContents())
81+
ResetMessageConsumed();
82+
83+
using (var reader = InternalGetReaderAtBodyContents())
8184
{
8285
writer.WriteNode(reader, true);
8386
}
8487
}
8588

8689
protected override XmlDictionaryReader OnGetReaderAtBodyContents()
90+
{
91+
ResetMessageConsumed();
92+
93+
return InternalGetReaderAtBodyContents();
94+
}
95+
96+
private XmlDictionaryReader InternalGetReaderAtBodyContents()
8797
{
8898
var reader = new XDocumentXmlReader(_body);
8999

@@ -175,5 +185,19 @@ private static MessageProperties ExtractSoapProperties(HttpRequest httpRequest)
175185

176186
return properties;
177187
}
188+
189+
private void ResetMessageConsumed()
190+
{
191+
var stateField = typeof(Message).GetProperty("State");
192+
if (stateField != null)
193+
{
194+
// Set the state to 'Created' to allow reuse
195+
stateField.SetValue(this, MessageState.Created);
196+
}
197+
else
198+
{
199+
throw new InvalidOperationException("Unable to access the internal state field.");
200+
}
201+
}
178202
}
179203
}

src/SoapCore/SoapCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>SOAP protocol middleware for ASP.NET Core</Description>
5-
<Version>1.2.1.6</Version>
5+
<Version>1.2.1.7</Version>
66
<Authors>Digital Design</Authors>
77
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0;netcoreapp3.1;</TargetFrameworks>
88
<PackageId>SoapCore</PackageId>

0 commit comments

Comments
 (0)