user3081814
user3081814

Reputation: 141

Inject header into WCF outbound message

I have a WCF service built on the classes created from a customer supplied WSDL. Unfortunately this WSDL did not contain the required message header. The client will not be supplying a new WSDL including the header. I do have an xsd file describing the header.

I also have a sample header and know which fields I need to populate.

How can I take this supplied header XML and inject it into an outbound WCF method call? I want to call my service method as I currently do, but I also want the new header structure to form part of the outbound message.

Thanks in advance. Any and all help will be greatly appreciated.

Here is an example of the message structure: I need to add the entire header structure. All that the WSDL contained was the body.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header>
      <glob:requestHeader xmlns:glob="http://....">
         <timestamp>2013-11-14T05:17:41.793+02:00</timestamp>
         <traceMessageId>GUID</traceMessageId>
         <enterpriseTraceUUId>GUID</enterpriseTraceUUId>
         <contentType>TEXT/XML</contentType>
         <sender>
            <senderId>SENDER</senderId>
            <sourceSystem>001</sourceSystem>
            <sourceApplication>001</sourceApplication>
            <applicationSessionId>ABC</applicationSessionId>
            <sourceLocation>100</sourceLocation>
         </sender>
         <interfaceName/>
         <version>1111</version>
      </glob:requestHeader>
   </s:Header>
   <s:Body xmlns:xsi="http://.../XMLSchema-instance" xmlns:xsd="http://.../XMLSchema">
      <UserData xmlns="http://.../Base">
         <IdField>1005687</IdField>
         <UserInfo>
            <UserType>1</UserType>
            <UserStatus>Y</UserStatus>
         </UserInfo>
      </UserData>
   </s:Body>
</s:Envelope>

Upvotes: 0

Views: 973

Answers (2)

Brian
Brian

Reputation: 3713

I used this, for instance, to add "User-Agent" to the header of my outgoing messages, but I think you could adapt it to your own needs:

private void AddCustomHeader(System.ServiceModel.OperationContextScope scope)
{
    dynamic reqProp = new System.ServiceModel.Channels.HttpRequestMessageProperty();
    reqProp.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT; blah; blah; blah)");
    System.ServiceModel.OperationContext.Current.OutgoingMessageProperties(System.ServiceModel.Channels.HttpRequestMessageProperty.Name) = reqProp;
}

I call this function above from the the constructor of the client-side program I use to call the host.

 AddCustomHeader(new System.ServiceModel.OperationContextScope(base.InnerChannel));

Probably the most important thing to notice is that it's adding this header variable to OutgoingMessageProperties of the "Current" OperationContext used by my client.

Upvotes: 1

Donal
Donal

Reputation: 32713

have you tried this? Also taken from here: How to add a custom HTTP header to every WCF call?

using (OperationContextScope scope = new OperationContextScope((IContextChannel)channel))
{
    MessageHeader<string> header = new MessageHeader<string>("secret message");
    var untyped = header.GetUntypedHeader("Identity", "http://www.my-website.com");
    OperationContext.Current.OutgoingMessageHeaders.Add(untyped);

    // now make the WCF call within this using block
}

Upvotes: 0

Related Questions