Skip to content

Commit

Permalink
Add "encodingStyle" attribute to generated SOAP message.
Browse files Browse the repository at this point in the history
* This fix was initially produced by @maheshnlrb with PR dotnet#3891 but the commit history got sufficiently scrambled that is was easier to reproduce his changes in a clean up-to-date branch.
  • Loading branch information
StephenBonikowsky committed Oct 29, 2019
1 parent 10c503a commit 8f35af5
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ internal class XmlSerializerOperationFormatter : OperationFormatter
private const string soap11Encoding = "http://schemas.xmlsoap.org/soap/encoding/";
private const string soap12Encoding = "http://www.w3.org/2003/05/soap-encoding";

private bool _isEncoded;
private MessageInfo _requestMessageInfo;
private MessageInfo _replyMessageInfo;

public XmlSerializerOperationFormatter(OperationDescription description, XmlSerializerFormatAttribute xmlSerializerFormatAttribute,
MessageInfo requestMessageInfo, MessageInfo replyMessageInfo) :
base(description, xmlSerializerFormatAttribute.Style == OperationFormatStyle.Rpc, xmlSerializerFormatAttribute.IsEncoded)
{
if (xmlSerializerFormatAttribute.IsEncoded && xmlSerializerFormatAttribute.Style != OperationFormatStyle.Rpc)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFxDocEncodedNotSupported, description.Name)));
}

_isEncoded = xmlSerializerFormatAttribute.IsEncoded;
_requestMessageInfo = requestMessageInfo;
_replyMessageInfo = replyMessageInfo;
}
Expand Down Expand Up @@ -83,13 +90,12 @@ protected override void AddHeadersToMessage(Message message, MessageDescription
MemoryStream memoryStream = new MemoryStream();
XmlDictionaryWriter bufferWriter = XmlDictionaryWriter.CreateTextWriter(memoryStream);
bufferWriter.WriteStartElement("root");
serializer.Serialize(bufferWriter, headerValues, null);
serializer.Serialize(bufferWriter, headerValues, null, _isEncoded ? GetEncoding(message.Version.Envelope) : null);
bufferWriter.WriteEndElement();
bufferWriter.Flush();
XmlDocument doc = new XmlDocument();
memoryStream.Position = 0;
doc.Load(memoryStream);
//doc.Save(Console.Out);
foreach (XmlElement element in doc.DocumentElement.ChildNodes)
{
MessageHeaderDescription matchingHeaderDescription = headerDescriptionTable.Get(element.LocalName, element.NamespaceURI);
Expand Down Expand Up @@ -236,7 +242,7 @@ protected override void GetHeadersFromMessage(Message message, MessageDescriptio
if (!bufferReader.IsEmptyElement)
{
bufferReader.ReadStartElement();
object[] headerValues = (object[])serializer.Deserialize(bufferReader);
object[] headerValues = (object[])serializer.Deserialize(bufferReader, _isEncoded ? GetEncoding(message.Version.Envelope) : null);
int headerIndex = 0;
foreach (MessageHeaderDescription headerDescription in messageDescription.Headers)
{
Expand Down Expand Up @@ -285,6 +291,12 @@ private static void AddUnknownHeader(MessageHeaderDescription unknownHeaderDescr

protected override void WriteBodyAttributes(XmlDictionaryWriter writer, MessageVersion version)
{
if (_isEncoded && version.Envelope == EnvelopeVersion.Soap11)
{
string encoding = GetEncoding(version.Envelope);
writer.WriteAttributeString("encodingStyle", version.Envelope.Namespace, encoding);
}

writer.WriteAttributeString("xmlns", "xsi", null, XmlUtil.XmlSerializerSchemaInstanceNamespace);
writer.WriteAttributeString("xmlns", "xsd", null, XmlUtil.XmlSerializerSchemaNamespace);
}
Expand Down Expand Up @@ -374,6 +386,7 @@ private void SerializeBody(XmlDictionaryWriter writer, MessageVersion version, X
bodyParameters[paramIndex++] = parameters[bodyParts[i].Index];
}

string encoding = _isEncoded ? GetEncoding(version.Envelope) : null;
serializer.Serialize(writer, bodyParameters, null);
}

Expand Down Expand Up @@ -444,7 +457,7 @@ private object DeserializeBody(XmlDictionaryReader reader, MessageVersion versio
return null;
}

object[] bodyParameters = (object[])serializer.Deserialize(reader);
object[] bodyParameters = (object[])serializer.Deserialize(reader, _isEncoded ? GetEncoding(version.Envelope) : null);
int paramIndex = 0;
if (IsValidReturnValue(returnPart))
{
Expand Down Expand Up @@ -568,7 +581,9 @@ internal void SetHeaderAttributes(MessageHeaderDescription headerDescription, bo
if (_attributes[headerDescription.Index] == null)
{
_attributes[headerDescription.Index] = new List<MessageHeader<object>>();
} ((List<MessageHeader<object>>)_attributes[headerDescription.Index]).Add(new MessageHeader<object>(null, mustUnderstand, actor, relay));
}

((List<MessageHeader<object>>)_attributes[headerDescription.Index]).Add(new MessageHeader<object>(null, mustUnderstand, actor, relay));
}
else
{
Expand Down

0 comments on commit 8f35af5

Please sign in to comment.