Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create a class that implements <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector.aspx" rel="nofollow noreferrer">IClientMessageInspector</a>. In the BeforeSendRequest method, add your custom header to the outgoing message. It might look something like this:</p> <pre><code>public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) { HttpRequestMessageProperty httpRequestMessage; object httpRequestMessageObject; if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject)) { httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty; if (string.IsNullOrEmpty(httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER])) { httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER] = this.m_userAgent; } } else { httpRequestMessage = new HttpRequestMessageProperty(); httpRequestMessage.Headers.Add(USER_AGENT_HTTP_HEADER, this.m_userAgent); request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage); } return null; } </code></pre> <p>Then create an endpoint behavior that applies the message inspector to the client runtime. You could apply the behavior as an attributes, or using a behavior extension element.</p> <p>Here is a great <a href="http://msmvps.com/blogs/paulomorgado/archive/2007/04/27/wcf-building-an-http-user-agent-message-inspector.aspx" rel="nofollow noreferrer">example</a> of how to add an HTTP user-agent header to all request messages. I am using this in a few of my clients. You could undoubtedly do something similar for just about any header. You can also do the same on the service side by implementing the <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector.aspx" rel="nofollow noreferrer">IDispatchMessageInspector</a>.</p> <p>Is this what you had in mind?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload