Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 消息头和XML_C#_Xml_Wcf_Soap - Fatal编程技术网

C# 消息头和XML

C# 消息头和XML,c#,xml,wcf,soap,C#,Xml,Wcf,Soap,我有一个xml文件。我必须将XMl文件添加到WCF请求的消息头中 我正在为此使用OperationContextScope using (OperationContextScope scope = new OperationContextScope(myClient.InnerChannel)) { var samlHeader = CreateSAMLAssertion(); OperationContext.Current.Ou

我有一个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>

我想在这里添加我的xml(smalheader)
编辑完成


有人能告诉我正确的方向吗?

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

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);
        }
    }
公共类StringXmlDataWriter:BodyWriter
{
私有字符串数据;
公共StringXmlDataWriter(字符串数据)
:base(假)
{
这个数据=数据;
}
受保护的重写无效OnWriteByContents(XmlDictionaryWriter编写器)
{
writer.WriteRaw(数据);
}
}
公共void ProcessHeaders()
{
字符串标题=“1337”;
var headerXML=XElement.Parse(headers);
foreach(headerXML.Elements()中的变量头)
{
var message=message.CreateMessage(OperationContext.Current.IncomingMessageVersion,header.Name.LocalName,新的StringXmlDataWriter(header.ToString());
OperationContext.Current.OutgoingMessageHeaders.CopyHeadersFrom(消息);
}
}

您想在WCF消息头中添加一个XML文件(如果使用SOAP,它本身就是XML)?谢谢您的回答。我上了一个叫SAMLAssertion的课。使断言元素成为类的属性。序列化类并添加到我的头中。OperationContext.Current.OutgoingMessageHeaders.Add(//序列化类)。我没有你的代码,因为这是我以前工作的一部分。谢谢