1

我有一个 xml 文件。我必须将 XMl 文件添加到 WCF 请求的消息头中。

我为此使用 OperationContextScope

using (OperationContextScope scope = new OperationContextScope(myClient.InnerChannel))
        {
            var samlHeader = CreateSAMLAssertion();
            OperationContext.Current.OutgoingMessageHeaders.Add(
                    // Add smalheader which is a xml hear

                );       
        } 

编辑:

samlHeader xml 看起来像这样

<Security xmlns="http://docs.oasis-open.org/x/xxxxx.xsd" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
 <Assertion ID="xxxxx" IssueInstant="xxxxxxx" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
 <--Removed-->
 </Assertion>
</Security>

我希望 SOAP 请求的结构看起来像这样

<soapenv:Envelope ........>
    <soapenv:Header>
          I want to add my xml (smalheader) here
    </soapenv:Header>
    <soapenv:Body>
    <soapenv:Body>
</soap:Envelope>

编辑完成

谁能指出我正确的方向

4

1 回答 1

0

将您的 XML 加载为 XElement(在我的情况下,它是您可以使用文件的字符串)。然后使用如下所示的 BodyWriter 类。我能够将 XML 转换为 Message 并以这种方式添加它们:

public class StringXmlDataWriter : BodyWriter
{
    private string data;

    public StringXmlDataWriter(string data)
        : base(false)
    {
        this.data = data;
    }

    protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
    {
        writer.WriteRaw(data);
    }
}

public void ProcessHeaders()
    {
        string headers = "<soapenv:Header xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"> <wsa:MessageID>1337</wsa:MessageID> </soapenv:Header>";

        var headerXML = XElement.Parse(headers);

        foreach (var header in headerXML.Elements())
        {
            var message = Message.CreateMessage(OperationContext.Current.IncomingMessageVersion, header.Name.LocalName, new StringXmlDataWriter(header.ToString()));
            OperationContext.Current.OutgoingMessageHeaders.CopyHeadersFrom(message);
        }
    }
于 2014-02-14T17:18:34.933 回答