1

What is the code equivalent of setting endpoint headers in configuration?

<client>
<endpoint address="http://localhost/..." >
<headers>
<something>blah</something>
</headers>
</endpoint>
Paul Hiles
  • 9,558
  • 7
  • 51
  • 76

2 Answers2

4

An alternative that doesn't require creating a new OperationContextScope and setting the header every time you use the client is to specify the headers when creating the EndpointAddress.

Example (adapted from https://stackoverflow.com/a/5340009/35233)

var binding = new WSHttpBinding ();
var address = new EndpointAddress (
    new Uri (RemoteAddress),
    new AddressHeader[] {
        AddressHeader.CreateAddressHeader ("APIKey", "", "bda11d91-7ade-4da1-855d-24adfe39d174")
    });

var client = new ExampleClient (binding, address);
Community
  • 1
  • 1
Chris Chilvers
  • 6,429
  • 3
  • 32
  • 53
2

This works:

var header = MessageHeader.CreateHeader("something", "", "blah");

using (new OperationContextScope(channel))
{
  OperationContext.Current.OutgoingMessageHeaders.Add(header);

  //your normal call here
}
Paul Hiles
  • 9,558
  • 7
  • 51
  • 76
  • 1
    Is there a way to assign these beforehand to the client or channelfactory without having to be in the current operation context? – kappasims Oct 31 '12 at 19:07