我为WCF服务提供了以下XML输入。使用XmlReader我正在验证消息并替换为新消息。在此过程中,xml名称空间别名从xmlns:soapenv
更改为xmlns:s
为了在重新创建消息时维护命名空间别名,需要在以下C#代码中进行哪些更改?
请参阅WCF message body showing <s:Body>... stream ...</s:Body> after modification查看正确的已更换邮件内容。
来自WCF Extensibility – Message Inspectors
WCF消息对象只能被“消费”一次 - 而“消费”可能意味着读取,写入或复制。消息体本质上是一次性读取流,因此一旦消耗它就不能再次使用。因此,如果在检查器代码中,有人要读取消息,那么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();
}
}
输出
<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>
答案 0 :(得分:2)
不,它没有改变命名空间。它正在将用于引用的前缀更改为命名空间,但在这两种情况下,命名空间本身都是"http://schemas.xmlsoap.org/soap/envelope/"
。
来自原始文件:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
从输出中:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
什