Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding http headers to a Service Reference service method

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?

like image 873
Shawn Doe Avatar asked Oct 07 '22 05:10

Shawn Doe


1 Answers

Borrowing from many places but mostly:

http://msmvps.com/blogs/paulomorgado/archive/2007/04/27/wcf-building-an-http-user-agent-message-inspector.aspx

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.

like image 85
Shawn Doe Avatar answered Oct 13 '22 12:10

Shawn Doe