I have generated a service reference from a WSDL. I have been coding a client against the service reference successfully. I have been using a pattern of serviceRef.serviceMethod(params...) to invoke the service based methods. Now I need to add http headers to my sent messages. I can't find where to set them as default for all messages for the service nor can I find where I can set them when invoking certain methods. Some articles suggest I could use IClientMessageInspector but the implementation seems complicated. Any suggestions?
Asked
Active
Viewed 7,564 times
1 Answers
5
Borrowing from many places but mostly:
I simplified but I think I have an implementation that will add custom httpheaders.
public class HttpHeaderMessageInspector : IClientMessageInspector
{
private readonly Dictionary<string, string> _httpHeaders;
public HttpHeaderMessageInspector(Dictionary<string, string> httpHeaders)
{
this._httpHeaders = httpHeaders;
}
public void AfterReceiveReply(ref Message reply, object correlationState) { }
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty httpRequestMessage;
object httpRequestMessageObject;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
foreach (var httpHeader in _httpHeaders)
{
httpRequestMessage.Headers[httpHeader.Key] = httpHeader.Value;
}
}
else
{
httpRequestMessage = new HttpRequestMessageProperty();
foreach (var httpHeader in _httpHeaders)
{
httpRequestMessage.Headers.Add(httpHeader.Key, httpHeader.Value);
}
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
}
return null;
}
}
internal class HttpHeadersEndpointBehavior : IEndpointBehavior
{
private readonly Dictionary<string,string> _httpHeaders;
public HttpHeadersEndpointBehavior(Dictionary<string, string> httpHeaders)
{
this._httpHeaders = httpHeaders;
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
var inspector = new HttpHeaderMessageInspector(this._httpHeaders);
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
public void Validate(ServiceEndpoint endpoint) { }
}
Then after newing up my service reference:
var httpHeaders = new Dictionary<string, string>();
httpHeaders.Add("header1", "value1");
httpHeaders.Add("header2", "value2");
_serviceRef.Endpoint.Behaviors.Add(new HttpHeadersEndpointBehavior(httpHeaders));
Nothing else had to change. If you think of a simpler way, let me know.
-
Put this solution here in the answer, not in the question. – Jul 18 '12 at 15:52
-
2http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.outgoingmessageheaders(v=vs.90).aspx is a simple solution. – cederlof Dec 05 '13 at 09:48
-
2@cederlof, your link is in reference to SOAP headers. The question is regarding HTTP headers. – NovaJoe Sep 29 '14 at 17:25