0

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>
Sam
  • 7,252
  • 16
  • 46
  • 65
user3081814
  • 141
  • 1
  • 1
  • 11
  • 2
    Have a look here: http://stackoverflow.com/questions/964433/how-to-add-a-custom-header-to-every-wcf-call – Donal Mar 26 '14 at 12:30
  • That is sort of what I am looking for. But in this instance I have a very specific header structure defined outside of WCF and which will be very difficult to model. I just want to take the XML header chunk and insert it into the message. – user3081814 Mar 26 '14 at 13:06

2 Answers2

1

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.

Brian
  • 3,653
  • 1
  • 22
  • 33
  • That could help. Do you have a more complete example I could look at. I would need to see how to wire this into my app. – user3081814 Mar 26 '14 at 13:17
  • Actually, what you see there is what I'm using. I only need it for the User-Agent element I'm adding. But I'm sure you could extrapolate that out to accommodate any number of needed headers you think are missing from your request. – Brian Mar 26 '14 at 13:25
0

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
}
Community
  • 1
  • 1
Donal
  • 31,121
  • 10
  • 63
  • 72