Repository navigation
Description
Summary
ParsedMessage.ExtractSoapBody throws System.InvalidOperationException: Duplicate attribute. when an incoming envelope declares a namespace prefix on both <Envelope> and <Body>. The request fails while the message is being read, before reaching any operation, and the caller gets a 500.
This is legal XML. The Namespaces in XML specification allows a descendant element to redeclare a prefix, and redeclaring it to the same URI is redundant rather than an error. Several SOAP toolkits emit this shape routinely, so services receiving traffic from third-party clients can be broken by it without any change on the client side.
Versions
Reproduced on 1.2.1.16. The same code is present in 1.2.1.12 through 1.2.1.16 and on develop.
We did not see this on 1.2.0. Our own regression appeared when upgrading 1.2.0 to 1.2.1.12, which is where the XDocument-based ParsedMessage reader replaced the previous envelope reading path.
Minimal repro
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:tns="urn:example">
<soapenv:Body xmlns:tns="urn:example">
<tns:Ping/>
</soapenv:Body>
</soapenv:Envelope>xmlns:tns appears on both elements, bound to the same URI. POSTing this to any SOAP 1.2 endpoint fails.
Stack trace
System.InvalidOperationException: Duplicate attribute.
at System.Xml.Linq.XElement.AddAttributeSkipNotify(XAttribute a)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at SoapCore.ParsedMessage.ExtractSoapBody(XDocument envelope, MessageVersion version) in /_/src/SoapCore/ParsedMessage.cs:line 194
at SoapCore.ParsedMessage.FromStreamAsync(Stream stream, Encoding readEncoding, MessageVersion version, CancellationToken ct) in /_/src/SoapCore/ParsedMessage.cs:line 84
at SoapCore.MessageEncoder.SoapMessageEncoder.ReadMessageAsync(Stream stream, Int32 maxSizeOfHeaders, String contentType, CancellationToken ct) in /_/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs:line 149
at SoapCore.SoapEndpointMiddleware`1.ReadMessageAsync(HttpContext httpContext, SoapMessageEncoder messageEncoder)
at SoapCore.SoapEndpointMiddleware`1.ProcessOperation(HttpContext httpContext, IServiceProvider serviceProvider)
Cause
In ExtractSoapBody:
var rootAttributes = root.Attributes();
var bodyAttributes = bodyNode.Attributes();
var allAttributes = bodyAttributes.Union(rootAttributes);
bodyNode.ReplaceAttributes(allAttributes);XAttribute does not override Equals, so Union falls back to reference equality. The xmlns:tns on <Envelope> and the one on <Body> are two distinct objects, both survive the union, and ReplaceAttributes then tries to add two attributes with the same name.
This reproduces standalone, with no SoapCore and no HTTP involved:
var envelope = XDocument.Parse(xml);
XNamespace soapNs = "http://www.w3.org/2003/05/soap-envelope";
var bodyNode = envelope.Root.Element(soapNs + "Body");
var allAttributes = bodyNode.Attributes().Union(envelope.Root.Attributes());
bodyNode.ReplaceAttributes(allAttributes); // InvalidOperationException: Duplicate attribute.Suggested fix
Prefer the body's own attributes, and take from the root only those whose name is not already present:
var allAttributes = bodyAttributes
.Concat(rootAttributes.Where(r => bodyNode.Attribute(r.Name) == null))
.ToList();
bodyNode.ReplaceAttributes(allAttributes);This also corrects a second, quieter problem. When <Body> redeclares a prefix bound to a different URI than <Envelope>, the body's binding has to win for the body subtree. The current Union keeps both attributes, so even in the cases where it does not throw, the resulting precedence is wrong. Preferring the body's attribute matches XML scoping rules.
.ToList() is not strictly required, since ReplaceAttributes snapshots its content before removing attributes, but it makes the evaluation order explicit.
Workaround
For anyone hitting this before a fix ships: normalize the request body in middleware ahead of the SOAP endpoint, dropping any namespace declaration already bound to the same prefix and URI by an ancestor element. Declarations that bind a prefix to a different URI have to be kept, or shadowing breaks.
Activity
github-actions commentedon Oct 19, 2026
This issue is stale because it has been open for 30 days with no activity.
Adding tests highlighting issue #1196
Fix duplicate attribute when the Body redeclares an Envelope namespac…
Merge pull request #1197 from falc0r/fix/1196-duplicate-namespace-att…