我想使用IClientMessageInspector
的BeforeSendRequest
方法"注入"自定义XML文本。这是我尝试的代码:
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;
}
但它确实返回了我的错误"未识别的消息版本"。
在操纵之前制作消息的副本:
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
Message message = buffer.CreateMessage();
然后根据您的XML创建新消息:
request = Message.CreateMessage(reader, int.MaxValue, message.Version);
您还可以创建新的按摩设置原始请求对象的版本:
Message newReply = Message.CreateMessage(reader, int.MaxValue, request.Version);
request = newReply;
希望它有帮助