我有一个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>
编辑完整
任何人都可以指出我正确的方向
答案 0 :(得分: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);
}
}