2012-07-16 2 views
4

WSDL에서 서비스 참조를 생성했습니다. 나는 서비스 레퍼런스에 대한 클라이언트를 성공적으로 코딩하고있다. serviceRef.serviceMethod (params ...) 패턴을 사용하여 서비스 기반 메소드를 호출했습니다. 이제 보낸 메시지에 http 헤더를 추가해야합니다. 서비스의 모든 메시지를 기본값으로 설정할 위치를 찾을 수 없으며 특정 방법을 호출 할 때 설정할 수있는 위치를 찾을 수 없습니다. 일부 기사에서는 IClientMessageInspector를 사용할 수 있다고 제안하지만 구현이 복잡해 보입니다. 어떤 제안?서비스 참조 서비스 메소드에 http 헤더 추가

답변

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) { } 
    } 

그런 다음 newing까지 내 서비스 참조를 한 후 : 다른

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

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

아무것도 변화가 없었다. 더 간단한 방법을 생각하면 알려주세요.

+0

이 솔루션을 질문이 아닌 답변에 입력하십시오. –

+1

http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.outgoingmessageheaders(v=vs.90).aspx는 간단한 솔루션입니다. – cederlof

+2

@cederlof, 귀하의 링크는 SOAP 헤더를 참조합니다. 질문은 HTTP 헤더에 관한 것입니다. – NovaJoe

관련 문제