I am working on a SOAP Service where I need to send out a formatted error message with <?xml version="1.0" encoding="UTF-8"?> at the top of outgoing message.
Successful responses do not have this version text at the top of the response.
The reason I am doing this is because we are taking over the provision of a system and the original supplier and have only been given the WSDL file.
I have a method that is looking to change the message to include this as below, however, when I put this into the new message <?xml version="1.0" encoding="UTF-8"?> get stripped out.
public Message ChangeString(Message oldMessage, string from, string to)
{
MemoryStream ms = new MemoryStream();
XmlWriter xw = XmlWriter.Create(ms);
oldMessage.WriteMessage(xw);
xw.Flush();
string body = Encoding.UTF8.GetString(ms.ToArray());
xw.Close();
body = body.Replace(from, to); //// Can see the body being changed here.
ms = new MemoryStream(Encoding.UTF8.GetBytes(body));
XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(ms, new XmlDictionaryReaderQuotas());
Message newMessage = Message.CreateMessage(xdr, int.MaxValue, oldMessage.Version);
newMessage.Properties.CopyProperties(oldMessage.Properties);
return newMessage;
}
Calling code is below
public void ProvideFault(Exception error, MessageVersion messageVersion, ref Message fault)
{
var faultException = new FaultException(error.Message);
var messageFault = faultException.CreateMessageFault();
var message = Message.CreateMessage(messageVersion, messageFault, null);
message = ChangeString(message, message.ToString(), $"<?xml version=\"1.0\" encoding=\"UTF-8\"?>{message.ToString()}");
fault = new ProposalMessage(message);
}
So, the question is, how do I get a Message out to the user with the correct xml encoding at the top?
The full response that I get is
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring xml:lang="en-GB">Unable to satisfy web service request at this time.This may relate to the format or sequence of the requests, the status of the requested information or reflect a service issue.</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
--- Edit --
var errorFormatter = new ErrorMessageFormatter(new CustomTextMessageEncoderFactory("text/xml", "UTF-8", messageVersion));
var errorByteArray = errorFormatter.WriteMessage(Message.CreateMessage(messageVersion, messageFault, null), int.MaxValue, BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue));
var message = errorFormatter.ReadMessage(errorByteArray, BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue));
fault = new ProposalMessage(message);
Is what I have added which then also removes SOAP-ENV from Fault too.
The version and encoding of the message are related to the binding used.We usually modify the encoding of the message in the binding,as follows:
We can also modify the message version and encoding by customizing the message encoder,as follows:
For more information about Custom Message Encoder,Please refer to the following link:
https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/custom-message-encoder-custom-text-encoder