Path: redb-route/redb.Route.Soap/SoapEnvelope.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Soap/SoapEnvelope.cs ---- using System.Text; using System.Xml.Linq; using redb.Route.Core; namespace redb.Route.Soap; /// Result of parsing a SOAP envelope: the body payload, or a fault. internal readonly record struct SoapParseResult(bool IsFault, string? FaultCode, string? FaultString, string BodyXml); /// /// Builds and parses SOAP 1.1 / 1.2 envelopes with (in-box, no WCF). /// Payload mode: the exchange body is the content of <soap:Body>. Faults are detected for /// both versions (1.1 faultcode/faultstring, 1.2 Code/Reason). /// internal static class SoapEnvelope { public const string Ns11 = "http://schemas.xmlsoap.org/soap/envelope/"; public const string Ns12 = "http://www.w3.org/2003/05/soap-envelope"; public static XNamespace Ns(SoapVersion v) => v == SoapVersion.Soap12 ? Ns12 : Ns11; /// The HTTP Content-Type for the version (SOAPAction is folded into it for 1.2). public static string ContentType(SoapVersion v, string? action) => v == SoapVersion.Soap12 ? $"application/soap+xml; charset=utf-8{(string.IsNullOrEmpty(action) ? "" : $"; action=\"{action}\"")}" : "text/xml; charset=utf-8"; /// /// Wraps (an XML fragment) in an envelope. Optional /// becomes the <soap:Header> content. /// public static byte[] Build(string bodyXml, SoapVersion version, string? headerXml = null) { var soap = Ns(version); var body = new XElement(soap + "Body"); if (!string.IsNullOrWhiteSpace(bodyXml)) body.Add(ParseBodyContent(bodyXml)); var envelope = new XElement(soap + "Envelope", new XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName)); if (!string.IsNullOrWhiteSpace(headerXml)) envelope.Add(new XElement(soap + "Header", ParseFragments(headerXml!))); envelope.Add(body); var doc = new XDocument(new XDeclaration("1.0", "utf-8", null), envelope); using var ms = new MemoryStream(); using (var writer = System.Xml.XmlWriter.Create(ms, new System.Xml.XmlWriterSettings { Encoding = new UTF8Encoding(false), OmitXmlDeclaration = false })) doc.Save(writer); return ms.ToArray(); } /// Parses a response/request envelope into its body payload or a fault. public static SoapParseResult Parse(byte[] envelope, SoapVersion version) { var doc = SafeXml.Load(envelope); var soap = Ns(version); var body = doc.Root?.Element(soap + "Body") ?? throw new FormatException("SOAP envelope has no ."); var fault = body.Element(soap + "Fault"); if (fault is not null) { if (version == SoapVersion.Soap12) { var code = fault.Element(soap + "Code")?.Element(soap + "Value")?.Value; var reason = fault.Element(soap + "Reason")?.Element(soap + "Text")?.Value; return new SoapParseResult(true, code, reason, fault.ToString()); } // 1.1: faultcode / faultstring live in the empty namespace. var faultcode = fault.Element("faultcode")?.Value; var faultstring = fault.Element("faultstring")?.Value; return new SoapParseResult(true, faultcode, faultstring, fault.ToString()); } var payload = body.Elements().FirstOrDefault(); return new SoapParseResult(false, null, null, payload?.ToString() ?? string.Empty); } /// Extracts the <soap:Header> child elements (the envelope header plane), or empty. public static IEnumerable ReadHeaders(byte[] envelope, SoapVersion version) { var doc = SafeXml.Load(envelope); var soap = Ns(version); var header = doc.Root?.Element(soap + "Header"); return header?.Elements() ?? Enumerable.Empty(); } /// /// Namespaces for the prefixes a fault code is most likely to use, so a route naming a standard /// fault does not have to repeat the URI every time. A prefix that is not here, and whose namespace /// the route did not give, is written as it was handed over — the alternative would be inventing a /// namespace for it, and a wrong namespace is worse than an unresolved prefix. /// private static readonly Dictionary WellKnownFaultPrefixes = new(StringComparer.Ordinal) { ["wst"] = "http://docs.oasis-open.org/ws-sx/ws-trust/200512", ["wsse"] = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", ["wsu"] = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", ["wsa"] = "http://www.w3.org/2005/08/addressing", ["wsrm"] = "http://docs.oasis-open.org/ws-rx/wsrm/200702", }; /// /// The fault code for "the caller sent something we cannot process", spelled for this SOAP version: /// soap:Sender in 1.2, soap:Client in 1.1. /// /// This is not cosmetic. SOAP 1.2 defines the other value, Receiver (the 1.1 Server), /// as a failure "attributable to the processing of the message rather than to the contents of the /// message itself" — in practice a retry hint. A request we could not even parse is the opposite: /// the same bytes will fail again, so a caller that branches on the code (and a retry handler is /// exactly such a caller) must be told the fault is its own, or it will re-send until it gives up. /// /// public static string SenderCode(SoapVersion version) => version == SoapVersion.Soap12 ? "soap:Sender" : "soap:Client"; /// /// Builds a SOAP Fault envelope (for a consumer returning an error). /// /// A fault code is a QName in both SOAP versions, so a prefixed code such as /// wst:FailedAuthentication is only meaningful when the prefix is bound on the envelope. It is /// declared here for that reason: a code whose prefix nothing declares is not a code a strict client /// can resolve, and the clients that branch on fault codes are exactly the strict ones. /// /// /// /// Namespace for the prefix in . Optional: standard prefixes are known, /// and this is for a code in a namespace of the caller's own. /// public static byte[] BuildFault(string faultString, SoapVersion version, string? faultCode = null, string? faultCodeNamespace = null) { var soap = Ns(version); var code = faultCode ?? (version == SoapVersion.Soap12 ? "soap:Receiver" : "soap:Server"); XElement fault = version == SoapVersion.Soap12 ? new XElement(soap + "Fault", new XElement(soap + "Code", new XElement(soap + "Value", code)), new XElement(soap + "Reason", new XElement(soap + "Text", faultString))) : new XElement(soap + "Fault", new XElement("faultcode", code), new XElement("faultstring", faultString)); var envelope = new XElement(soap + "Envelope", new XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName), new XElement(soap + "Body", fault)); // The `soap` prefix is already bound above, so a soap:* code needs nothing further. if (ResolveFaultPrefix(code, faultCodeNamespace) is var (prefix, ns) && prefix is not null) envelope.Add(new XAttribute(XNamespace.Xmlns + prefix, ns!)); return Encoding.UTF8.GetBytes(new XDocument(envelope).ToString(SaveOptions.DisableFormatting)); } /// /// Works out which prefix a fault code uses and what to bind it to, or nothing when there is no /// prefix, when it is already bound, or when its namespace is unknown and was not supplied. /// private static (string? Prefix, string? Namespace) ResolveFaultPrefix(string code, string? supplied) { var colon = code.IndexOf(':'); if (colon <= 0) return (null, null); var prefix = code[..colon]; if (prefix == "soap") return (null, null); if (!string.IsNullOrEmpty(supplied)) return (prefix, supplied); return WellKnownFaultPrefixes.TryGetValue(prefix, out var ns) ? (prefix, ns) : (null, null); } private static object ParseBodyContent(string xml) { // The common case is a single Body child element; XElement.Parse also skips a leading XML prolog. try { return SafeXml.ParseElement(xml, LoadOptions.PreserveWhitespace); } // A document/literal body may legitimately carry several sibling elements — wrap-parse and add all. catch (System.Xml.XmlException) { return ParseFragments(xml).Cast().ToArray(); } } /// Parses one or more sibling XML elements (e.g. several <soap:Header> children). private static IEnumerable ParseFragments(string xml) { // Wrap so multiple top-level elements parse; each element carries its own inline xmlns. var wrapped = SafeXml.ParseElement($"{xml}", LoadOptions.PreserveWhitespace); return wrapped.Elements(); } }