Thursday, January 17, 2013

WCF Adding HttpHeaders to ChannelFactory request

To add credentials to WCF ChannelFactory request:
Create Set up ChannelFactory with desired binding:
ChannelFacotry<ITest>factory = new ChannelFactory<ITest>(new BasicHttpBinding(BasicHttpSecurityMode.None));

Create proxy using EndpointAddress:
ITest proxy = factory.CreateChannel(new EndpointAddress(TheEndpointAddress));
Create OperationContextScope with the proxy:
OperationContextScope contextScope = new OperationContextScope(proxy)
Create HttpRequestMessageProperty and configure credentials:
HttpRequestMessageProperty httpHeaders = new HttpRequestMessageProperty();
httpHeaders.Headers[USERNAME_HTTP_HEADER] = Username;

httpHeaders.Headers[PASSWORD_HTTP_HEADER] = Password;

Add HttpRequestMessageProperty to the OperationContexts outgoing message properties:
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpHeaders;

1 comment:

Anonymous said...

Saved my life!