What is the code equivalent of setting endpoint headers in configuration?
<client>
<endpoint address="http://localhost/..." >
<headers>
<something>blah</something>
</headers>
</endpoint>
What is the code equivalent of setting endpoint headers in configuration?
<client>
<endpoint address="http://localhost/..." >
<headers>
<something>blah</something>
</headers>
</endpoint>
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);
This works:
var header = MessageHeader.CreateHeader("something", "", "blah");
using (new OperationContextScope(channel))
{
OperationContext.Current.OutgoingMessageHeaders.Add(header);
//your normal call here
}