将http标头添加到服务引用服务方法

时间:2012-07-16 17:02:25

标签: .net http-headers service-reference

我已经从WSDL生成了一个服务引用。我一直在成功地对服务引用编写客户端。我一直在使用serviceRef.serviceMethod(params ...)模式来调用基于服务的方法。现在我需要将http标头添加到我发送的消息中。我找不到将它们设置为服务的所有消息的默认位置,也无法在调用某些方法时找到我可以设置它们的位置。一些文章建议我可以使用IClientMessageInspector,但实现似乎很复杂。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

从很多地方借来,但主要是:

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

我简化但我认为我有一个实现,它将添加自定义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) { }
    }

然后在更新我的服务参考后:

    var httpHeaders = new Dictionary<string, string>();
    httpHeaders.Add("header1", "value1");
    httpHeaders.Add("header2", "value2");

    _serviceRef.Endpoint.Behaviors.Add(new HttpHeadersEndpointBehavior(httpHeaders));

没有别的东西可以改变。如果你想一个更简单的方法,请告诉我。