开发者

setting wcf endpoint headers in code

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

<client>
<endpoint address="http://localhost/..." >
<headers>
<something>blah</something>
开发者_JAVA技巧</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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜