将HTTP标头添加到服务参考服务方法

Sha*_*Doe 5 .net service-reference http-headers

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

Sha*_*Doe 5

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

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) { }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

    _serviceRef.Endpoint.Behaviors.Add(new HttpHeadersEndpointBehavior(httpHeaders));
Run Code Online (Sandbox Code Playgroud)

其他什么都不需要改变。如果您想到更简单的方法,请告诉我。

  • http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.outgoingmessageheaders(v=vs.90).aspx 是一个简单的解决方案。 (2认同)
  • @cederlof,您的链接引用了 SOAP 标头。问题是关于 HTTP 标头。 (2认同)