I'd like to add an HTTP header with user token each time the iOs client calls the WCF service. I found two way for doing it more or less transparently: via WebOperationContext
and via an implementation of IClientMessageInspector
. None of those has worked for me yet, though.
I could not bind the message inspector to an Endpoint via IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
because the property ClientRuntime.ClientMessageInspectors throws the NotImplementedException.
WebOperationContext did not help me either. The following code of Windows console app works well, the header is read by the service.
var channelFactory = new ChannelFactory<IDataService>(binding, endPointAddress);
IDataService client = channelFactory.CreateChannel();
using (new OperationContextScope((IClientChannel)client))
{
WebOperationContext woc = new WebOperationContext(OperationContext.Current);
woc.OutgoingRequest.Headers["X-Test42"] = "header value";
var data = client.GetData();
}
The code for the iOs differs a bit because I can't use ChannelFactory for dynamic proxy generation. I created implementations of ClientBase<IDataService>
and ClientBase<IDataService>.ChannelBase<IDataService>
according to
https://stackoverflow.com/a/10056431/456181
The property DataServiceClient.ContextChannel returns the instance of ClientBase<IDataService>.ChannelBase<IDataService>
.
DataServiceClient client = new DataServiceClient(binding, endPointAddress);
using (new OperationContextScope(client.ContextChannel))
{
WebOperationContext woc = new WebOperationContext(OperationContext.Current);
woc.OutgoingRequest.Headers["X-Test42"] = "header value";
var data = client.GetData();
}
The header "X-Test42" does not get to the WCF service.
Can anybody suggest how to adjust any of those two methods or any other method of adding an HTTP header?