public static void WriteHeaders(XmlWriter writer, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool isEncoded, string defaultNS, bool serviceDefaultIsEncoded, string envelopeNS) { if (headers.Count == 0) { return; } writer.WriteStartElement(Soap.Element.Header, envelopeNS); SoapProtocolVersion version; string encodingStyle; if (envelopeNS == Soap12.Namespace) { version = SoapProtocolVersion.Soap12; encodingStyle = Soap12.Encoding; } else { version = SoapProtocolVersion.Soap11; encodingStyle = Soap.Encoding; } int unknownHeaderCount = 0; ArrayList otherHeaders = new ArrayList(); SoapHeader[] headerArray = new SoapHeader[mappings.Length]; bool[] headerSet = new bool[headerArray.Length]; for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header == null) { continue; } int headerPosition; header.version = version; if (header is SoapUnknownHeader) { otherHeaders.Add(header); unknownHeaderCount++; } else if ((headerPosition = FindMapping(mappings, header, direction)) >= 0 && !headerSet[headerPosition]) { headerArray[headerPosition] = header; headerSet[headerPosition] = true; } else { otherHeaders.Add(header); } } int otherHeaderCount = otherHeaders.Count - unknownHeaderCount; if (isEncoded && otherHeaderCount > 0) { SoapHeader[] newHeaderArray = new SoapHeader[mappings.Length + otherHeaderCount]; headerArray.CopyTo(newHeaderArray, 0); // fill in the non-statically known headers (otherHeaders) starting after the statically-known ones int count = mappings.Length; for (int i = 0; i < otherHeaders.Count; i++) { if (!(otherHeaders[i] is SoapUnknownHeader)) { newHeaderArray[count++] = (SoapHeader)otherHeaders[i]; } } headerArray = newHeaderArray; } TraceMethod caller = Tracing.On ? new TraceMethod(typeof(SoapHeaderHandling), "WriteHeaders") : null; if (Tracing.On) { Tracing.Enter(Tracing.TraceId(Res.TraceWriteHeaders), caller, new TraceMethod(serializer, "Serialize", writer, headerArray, null, isEncoded ? encodingStyle : null, "h_")); } serializer.Serialize(writer, headerArray, null, isEncoded ? encodingStyle : null, "h_"); if (Tracing.On) { Tracing.Exit(Tracing.TraceId(Res.TraceWriteHeaders), caller); } foreach (SoapHeader header in otherHeaders) { if (header is SoapUnknownHeader) { SoapUnknownHeader unknown = (SoapUnknownHeader)header; if (unknown.Element != null) { unknown.Element.WriteTo(writer); } } else if (!isEncoded) // encoded headers already appended to members mapping { string ns = SoapReflector.GetLiteralNamespace(defaultNS, serviceDefaultIsEncoded); XmlSerializer headerSerializer = new XmlSerializer(header.GetType(), ns); if (Tracing.On) { Tracing.Enter(Tracing.TraceId(Res.TraceWriteHeaders), caller, new TraceMethod(headerSerializer, "Serialize", writer, header)); } headerSerializer.Serialize(writer, header); if (Tracing.On) { Tracing.Exit(Tracing.TraceId(Res.TraceWriteHeaders), caller); } } } // reset the soap version for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header != null) { header.version = SoapProtocolVersion.Default; } } writer.WriteEndElement(); writer.Flush(); }
internal static void WriteHeaders(XmlWriter writer, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool isEncoded, string defaultNs, bool serviceDefaultIsEncoded, string envelopeNs) { if (headers.Count == 0) { return; } if (isEncoded && writer is XmlSpecialTextWriter) { ((XmlSpecialTextWriter)writer).EncodeIds = true; } writer.WriteStartElement(Soap.Header, envelopeNs); // SOAP12: always soap 1.1, not using encodingStyle; //SoapProtocolVersion version; SoapProtocolVersion version = SoapProtocolVersion.Soap11; // SOAP12: not using encodingStyle /*string encodingStyle; * if (envelopeNs == Soap12.Namespace) { * version = SoapProtocolVersion.Soap12; * encodingStyle = Soap12.Encoding; * } * else { * version = SoapProtocolVersion.Soap11; * encodingStyle = Soap.Encoding; * }*/ int unknownHeaderCount = 0; ArrayList otherHeaders = new ArrayList(); SoapHeader[] headerArray = new SoapHeader[mappings.Length]; bool[] headerSet = new bool[headerArray.Length]; for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header == null) { continue; } int headerPosition; header.version = version; if (header is SoapUnknownHeader) { otherHeaders.Add(header); unknownHeaderCount++; } else if ((headerPosition = FindMapping(mappings, header, direction)) >= 0 && !headerSet[headerPosition]) { headerArray[headerPosition] = header; headerSet[headerPosition] = true; } else { otherHeaders.Add(header); } } int otherHeaderCount = otherHeaders.Count - unknownHeaderCount; if (isEncoded && otherHeaderCount > 0) { SoapHeader[] newHeaderArray = new SoapHeader[mappings.Length + otherHeaderCount]; headerArray.CopyTo(newHeaderArray, 0); // fill in the non-statically known headers (otherHeaders) starting after the statically-known ones int count = mappings.Length; for (int i = 0; i < otherHeaders.Count; i++) { if (!(otherHeaders[i] is SoapUnknownHeader)) { newHeaderArray[count++] = (SoapHeader)otherHeaders[i]; } } headerArray = newHeaderArray; } // SOAP12: not using encodingStyle //serializer.Serialize(writer, headerArray, null, isEncoded ? encodingStyle : null); serializer.Serialize(writer, headerArray, null); foreach (SoapHeader header in otherHeaders) { if (header is SoapUnknownHeader) { SoapUnknownHeader unknown = (SoapUnknownHeader)header; if (unknown.Element != null) { unknown.Element.WriteTo(writer); } } else if (!isEncoded) // encoded headers already appended to members mapping { string ns = SoapReflector.GetLiteralNamespace(defaultNs, serviceDefaultIsEncoded); new XmlSerializer(header.GetType(), ns).Serialize(writer, header); } } // reset the soap version for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header != null) { header.version = SoapProtocolVersion.Default; } } writer.WriteEndElement(); writer.Flush(); if (isEncoded && writer is XmlSpecialTextWriter) { ((XmlSpecialTextWriter)writer).EncodeIds = false; } }
public static void WriteHeaders(XmlWriter writer, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool isEncoded, string defaultNS, bool serviceDefaultIsEncoded, string envelopeNS) { if (headers.Count != 0) { SoapProtocolVersion version; string str; writer.WriteStartElement("Header", envelopeNS); if (envelopeNS == "http://www.w3.org/2003/05/soap-envelope") { version = SoapProtocolVersion.Soap12; str = "http://www.w3.org/2003/05/soap-encoding"; } else { version = SoapProtocolVersion.Soap11; str = "http://schemas.xmlsoap.org/soap/encoding/"; } int num = 0; ArrayList list = new ArrayList(); SoapHeader[] o = new SoapHeader[mappings.Length]; bool[] flagArray = new bool[o.Length]; for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header != null) { header.version = version; if (header is SoapUnknownHeader) { list.Add(header); num++; } else { int num3; if (((num3 = FindMapping(mappings, header, direction)) >= 0) && !flagArray[num3]) { o[num3] = header; flagArray[num3] = true; } else { list.Add(header); } } } } int num4 = list.Count - num; if (isEncoded && (num4 > 0)) { SoapHeader[] array = new SoapHeader[mappings.Length + num4]; o.CopyTo(array, 0); int length = mappings.Length; for (int k = 0; k < list.Count; k++) { if (!(list[k] is SoapUnknownHeader)) { array[length++] = (SoapHeader)list[k]; } } o = array; } TraceMethod caller = Tracing.On ? new TraceMethod(typeof(SoapHeaderHandling), "WriteHeaders", new object[0]) : null; if (Tracing.On) { object[] args = new object[5]; args[0] = writer; args[1] = o; args[3] = isEncoded ? str : null; args[4] = "h_"; Tracing.Enter(Tracing.TraceId("TraceWriteHeaders"), caller, new TraceMethod(serializer, "Serialize", args)); } serializer.Serialize(writer, o, null, isEncoded ? str : null, "h_"); if (Tracing.On) { Tracing.Exit(Tracing.TraceId("TraceWriteHeaders"), caller); } foreach (SoapHeader header2 in list) { if (header2 is SoapUnknownHeader) { SoapUnknownHeader header3 = (SoapUnknownHeader)header2; if (header3.Element != null) { header3.Element.WriteTo(writer); } } else if (!isEncoded) { string literalNamespace = SoapReflector.GetLiteralNamespace(defaultNS, serviceDefaultIsEncoded); XmlSerializer target = new XmlSerializer(header2.GetType(), literalNamespace); if (Tracing.On) { Tracing.Enter(Tracing.TraceId("TraceWriteHeaders"), caller, new TraceMethod(target, "Serialize", new object[] { writer, header2 })); } target.Serialize(writer, header2); if (Tracing.On) { Tracing.Exit(Tracing.TraceId("TraceWriteHeaders"), caller); } } } for (int j = 0; j < headers.Count; j++) { SoapHeader header4 = headers[j]; if (header4 != null) { header4.version = SoapProtocolVersion.Default; } } writer.WriteEndElement(); writer.Flush(); } }