I would like to "inject" a custom XML text using BeforeSendRequest
method from IClientMessageInspector
. Here is the code I tried:
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
String myXML = "somexmlcontent";
XmlDocument doc = new XmlDocument();
doc.LoadXml(myXML);
var ms = new MemoryStream();
XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(ms);
doc.WriteTo(writer);
writer.Flush();
ms.Position = 0;
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
Message newReply = Message.CreateMessage(reader, int.MaxValue, request.Version);
request = newReply;
return null;
}
But it does return me the error "Unrecognized message version".
Make a copy of message prior to manipulate:
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
Message message = buffer.CreateMessage();
Then create the new message based on your xml:
request = Message.CreateMessage(reader, int.MaxValue, message.Version);
You can also create the new massage setting the version from original request object:
Message newReply = Message.CreateMessage(reader, int.MaxValue, request.Version);
request = newReply;
Hope it helps