我有一个WCF服务的XML输入。使用XmlReader我正在验证消息并用新消息替换。在此过程中,xml名称空间别名从xmlns:soapenv
更改为xmlns:s
XmlDictionaryReader更改xml命名空间别名
为了在重新创建消息时维护命名空间别名,以下C#代码需要做哪些修改?
请参阅WCF message body showing <s:Body>... stream ...</s:Body> after modification查看正确的替换消息内容。
从WCF Extensibility – Message Inspectors
WCF Message对象只能是“消费”一次 - “消耗”可以指读,写或复制。消息体本质上是一次只读的流,所以一旦消耗,它就不能再被使用。因此,如果在检查器代码中读取消息,那么WCF运行时将无法在其管道的其余部分中重用该消息(即将其编码以作为回复进行发送或将其解析为操作参数)。因此,如果检查员代码需要阅读消息,检查员有责任重新创建消息。
输入
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header>
<To soapenv:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://local:54956/Service1.svc</To>
<Action soapenv:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService1/GetData</Action>
</soapenv:Header>
<soapenv:Body>
<tem:GetData>
<!--Optional:-->
<tem:value>4</tem:value>
</tem:GetData>
</soapenv:Body>
</soapenv:Envelope>
CODE
private void MyInspectorsValidateMessageBody(ref System.ServiceModel.Channels.Message message, bool isARequest)
{
string originalMessageText = message.ToString();
if (!message.IsFault)
{
XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
XmlReader bodyReader = message.GetReaderAtBodyContents().ReadSubtree();
//Settings
XmlReaderSettings wrapperSettings = new XmlReaderSettings();
wrapperSettings.CloseInput = true;
wrapperSettings.ValidationFlags = XmlSchemaValidationFlags.None;
wrapperSettings.ValidationType = ValidationType.Schema;
//Add a event handler for ValidationEventHandler of XmlReaderSettings
//Validation happens while read of xml instance
//wrapperSettings.ValidationEventHandler += new ValidationEventHandler(MyHandlerForXMLInspectionErrors);
XmlReader wrappedReader = XmlReader.Create(bodyReader, wrapperSettings);
this.isRequest = isARequest;
MemoryStream memStream = new MemoryStream();
XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateBinaryWriter(memStream);
xdw.WriteNode(wrappedReader, false);
xdw.Flush(); memStream.Position = 0;
XmlDictionaryReader xdr = XmlDictionaryReader.CreateBinaryReader(memStream, quotas);
//Reconstruct the message with the validated body
Message replacedMessage = Message.CreateMessage(message.Version, null, xdr);
replacedMessage.Headers.CopyHeadersFrom(message.Headers);
replacedMessage.Properties.CopyProperties(message.Properties);
message = replacedMessage;
string replacedMessageText = replacedMessage.ToString();
}
}
OUTPUT
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://local:54956/Service1.svc</To>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService1/GetData</Action>
</s:Header>
<s:Body>... stream ...</s:Body>
</s:Envelope>
是的,你是对的。但我需要维护'alias'。我已经更新了这个问题。 – Lijo 2013-03-25 16:40:01
@Lijo:*为什么* - 会在乎什么?无论如何是破坏IMO。 – 2013-03-25 16:41:05
我有一个自定义验证检查是否存在xmlns:soapenv。 – Lijo 2013-03-25 16:41:59