我有定製IClientMessageFormatter,目前增加歸因於指定的XML元素消息:無法獲得IClientMessageFormatter工作
public class GetOrdersMessageFormatter : IClientMessageFormatter
{
readonly IClientMessageFormatter original;
public GetOrdersMessageFormatter(IClientMessageFormatter actual)
{
original = actual;
}
public void AddArrayNamespace(XmlNode node)
{
if (node != null)
{
var attribute = node.OwnerDocument.CreateAttribute("test");
attribute.Value = "test";
node.Attributes.Append(attribute);
}
}
public object DeserializeReply(Message message, object[] parameters)
{
return original.DeserializeReply(message, parameters);
}
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
Message newMessage = null;
var message = original.SerializeRequest(messageVersion, parameters);
if (message.Headers.Action == "urn:Mage_Api_Model_Server_HandlerAction")
{
var doc = new XmlDocument();
using (var reader = message.GetReaderAtBodyContents())
{
doc.Load(reader);
}
if (doc.DocumentElement != null)
{
switch (doc.DocumentElement.LocalName)
{
case "call":
AddArrayNamespace(doc.SelectSingleNode("//args"));
break;
}
}
using (var ms = new MemoryStream())
{
using (var xw = XmlWriter.Create(ms))
{
doc.Save(xw);
ms.Position = 0;
using (var xr = XmlReader.Create(ms))
{
newMessage = Message.CreateMessage(message.Version, null, xr);
newMessage.Headers.CopyHeadersFrom(message);
newMessage.Properties.CopyProperties(message.Properties);
}
}
}
}
return newMessage;
}
}
它給例外
System.ArgumentException:該用於消息正文的XmlReader必須位於元素上。 參數名:讀者
服務器堆棧跟蹤:在在System.ServiceModel.Channels System.ServiceModel.Channels.BodyWriter.WriteBodyContents(的XmlDictionaryWriter作家) 在System.ServiceModel.Channels.XmlReaderBodyWriter.OnWriteBodyContents(的XmlDictionaryWriter作家) .BodyWriterMessage.OnWriteBodyContents(的XmlDictionaryWriter作家) 在System.ServiceModel.Channels.Message.OnWriteMessage(的XmlDictionaryWriter作家) 在System.ServiceModel.Channels.Message.WriteMessage(的XmlDictionaryWriter作家) 在System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(消息消息,BufferManager bufferManager,Int32 initialOffset,Int32 maxSizeQuota) 在System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.WriteMessage(消息消息的Int32 maxMessageSize,BufferManager bufferManager,的Int32 messageOffset) 在System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(消息信息) 在System.ServiceModel.Channels.HttpOutput 。發送(時間跨度超時) 在System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.SendRequest(消息消息,時間跨度超時) 在System.ServiceModel.Channels.RequestChannel.Request(消息消息,時間跨度超時) 在系統。 ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超時) at System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins ,對象[]奏,時間跨度超時) 在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage包括MethodCall,ProxyOperationRuntime操作) 在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(即時聊天消息)
我想,這可能是我需要使用message.CreateBufferedCopy()創建郵件副本並使用該副本來加載XmlDocument,但這也沒有奏效。 可能有人知道我做錯了什麼,或知道例子,這是做了非常相同的事情(我的意思是在發送之前影響消息xml)。