[英]Creating custom MessageHeader with namespace in WCF
I need to build the following Receipts XML structure including the xmlns:u namespace and add it to the SOAP header. 我需要构建以下包含XMLns:u命名空间的Receipts XML结构并将其添加到SOAP标头中。 So the end outgoing SOAP header should look something like this:
因此,最后的传出SOAP标头应如下所示:
<Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<s:Header>
<u:Receipts xmlns:u="http://MyCompany/abc">
<Receipt>
<Id />
<Text />
</Receipt>
<Receipt>
<Id />
<Text />
</Receipt>
<Receipt>
<Id />
<Text />
</Receipt>
</u:Receipts>
</s:Header>
<s:Body />
</Envelope>
I'm overriding the MessageHeader class and build the xml in OnWriteHeaderContents method. 我正在重写MessageHeader类,并在OnWriteHeaderContents方法中构建xml。 However, I can't get the correct xml/namespace.
但是,我无法获得正确的xml /命名空间。 Code sample is appreciated!
代码样例表示赞赏!
This is our solution at the moment, which works fine. 目前,这是我们的解决方案,效果很好。
public class ReceiptsHeader : MessageHeader
{
private const string HeaderName = "Receipts";
private const string HeaderNamespace = "http://MyCompany/abc";
public override string Name => HeaderName;
public override string Namespace => HeaderNamespace;
private readonly XmlDocument _usageReceipt = new XmlDocument();
public ReceiptsHeader(IEnumerable elements)
{
var headerString = new StringBuilder("<Receipts xmlns=\"http://MyCompany/abc\">");
foreach (var elem in elements)
{
// Build <Receipt> nodes here...
}
headerString.Append("</Receipts>");
_usageReceipt.LoadXml(headerString.ToString());
}
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
foreach (XmlNode node in _usageReceipt.ChildNodes[0].ChildNodes)
writer.WriteNode(node.CreateNavigator(), false);
}
}
We're then adding this to the SOAP header in the BeforeSendReply method, something like this: 然后,将其添加到BeforeSendReply方法的SOAP标头中,如下所示:
ReceiptsHeader head = new ReceiptsHeader(elements);
reply.Headers.Add(head);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.