5

WSDL からサービス参照を生成しました。サービス参照に対してクライアントを正常にコーディングしています。serviceRef.serviceMethod(params...) のパターンを使用して、サービスベースのメソッドを呼び出しています。ここで、送信したメッセージに http ヘッダーを追加する必要があります。サービスのすべてのメッセージのデフォルトとして設定する場所が見つからないか、特定のメソッドを呼び出すときに設定できる場所がわかりません。一部の記事では、IClientMessageInspector を使用できると示唆していますが、実装は複雑に見えます。助言がありますか?

4

1 に答える 1

4

多くの場所から借りていますが、主に:

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));

他に何も変更する必要はありませんでした。もっと簡単な方法があれば教えてください。

于 2012-07-18T14:25:59.130 に答える