I have an xml file. I have to add the XMl file to message header of WCF request.
I am using OperationContextScope for this
using (OperationContextScope scope = new OperationContextScope(myClient.InnerChannel))
{
var samlHeader = CreateSAMLAssertion();
OperationContext.Current.OutgoingMessageHeaders.Add(
// Add smalheader which is a xml hear
);
}
edit:
samlHeader xml looks like this
<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>
I want the struct of the SOAP request to look like this
<soapenv:Envelope ........>
<soapenv:Header>
I want to add my xml (smalheader) here
</soapenv:Header>
<soapenv:Body>
<soapenv:Body>
</soap:Envelope>
edit complete
Can any one please point me to the right direction
Load your XML as an XElement (in my case it's a string you could use a file). Then using a BodyWriter class like below. I was able to convert the XML into a Message and add them that way:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.