2012-04-11 4 views
1

내 문제가 있습니다. 서비스가 asmx 인 동안 생성 된 wcf 프록시 (서비스 참조)를 사용하여 웹 서비스 응답의 헤더를 가져올 수 없습니다. SOAP 응답이 약간의 헤더를 가지고 있기 때문에WCF 클라이언트를 사용하여 응답에서 사용자 지정 SOAP 헤더를 읽는 방법

ManagerSoapClient client = new ManagerSoapClient(); 
client.Authenticate(...); 
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) 
{ 
    //headers = null 
    var headers = OperationContext.Current.IncomingMessageHeaders; 
} 

그것은, 이상하다 :

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Header> 
     <OperationResult xmlns="http://tempuri.org/"> 
     <Status>Success</Status> 
     </OperationResult> 
    </soap:Header> 
    <soap:Body> 
     ... 
    </soap:Body> 
</soap:Envelope> 

확인을, 내가 헤더를 확인하는 사용자 정의 IClientMessageInspector을 추가 :

public class OperationResultMessageInspector : IClientMessageInspector 
{ 
    public object BeforeSendRequest(ref Message request, IClientChannel channel) 
    {   
     return channel; 
    } 

    public void AfterReceiveReply(ref Message reply, object correlationState) 
    { 
     //It founds header at position 0!! 
     int headerIndex = reply.Headers.FindHeader("OperationResult", "http://tempuri.org/"); 
    } 
} 

그래서 모두의 헤딩슛이 .. 하지만 왜 OperationContext.Current.IncomingMessageHeaders을 사용하여 액세스 할 수 없습니까?

확인. 이제이 헤더를 서비스 호출 후 액세스 할 수 있도록 일부 위치에 저장하려고합니다. 나는 그래서 내 코드는 지금 옆에 확장 IExtension<OperationContext>
을 사용하기로 결정 :

public class ManagerProxy 
{ 
    public void Authenticate() 
    { 
     ManagerSoapClient client = new ManagerSoapClient(); 
     client.Endpoint.Behaviors.Add(new OperationResultBehavior()); 
     client.Authenticate(...); 

     using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) 
     { 
      //headers = null 
      var headers = OperationContext.Current.IncomingMessageHeaders; 
      //header = null 
      OperationResultContextExtension header = OperationContext.Current.Extensions.Find<OperationResultContextExtension>(); 
     } 
    } 
} 

public class OperationResultMessageInspector : IClientMessageInspector 
{ 
    public object BeforeSendRequest(ref Message request, IClientChannel channel) 
    {   
     return channel; 
    } 

    public void AfterReceiveReply(ref Message reply, object correlationState) 
    { 
     IClientChannel channel = (IClientChannel)correlationState; 

     //It founds header at position 0!! 
     int headerIndex = reply.Headers.FindHeader("OperationResult", "http://tempuri.org/"); 
     XmlDictionaryReader reader = reply.Headers.GetReaderAtHeader(headerIndex); 

     OperationResultContextExtension extension = new OperationResultContextExtension 
     { 
      SomeData = reader.ReadString() 
     };   

     using (OperationContextScope scope = new OperationContextScope(channel)) 
     {    
      OperationContext.Current.Extensions.Add(extension); 
     } 
     reader.Close(); 
    } 

} 

public class OperationResultContextExtension : IExtension<OperationContext> 
{ 
    public string SomeData { get; set; } 
    public void Attach(OperationContext owner) { } 
    public void Detach(OperationContext owner) { } 
} 

public class OperationResultBehavior : IEndpointBehavior 
{ 
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } 
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { } 
    public void Validate(ServiceEndpoint endpoint) { } 
    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
    { clientRuntime.MessageInspectors.Add(new OperationResultMessageInspector()); } 
} 

그것은 작동해야하지만 예기치 않게 나 OperationContext.Current.Extensions.Find<OperationResultContextExtension>();에 대한뿐만 아니라 null입니다.

그래서 내 질문이 있습니다 :
1. 왜 OperationContext.Current.IncomingMessageHeaders 반환 null 내가 OperationContext.Current.Extensions.Find<OperationResultContextExtension>() 동안 null을받을 이유 확장 OperationResultContextExtensionAfterReceiveReply
2. 내부 제대로 reply.Headers.FindHeader 발견 헤더는 꽤 부드러운 보이는 동안.
3. 응답에서이 헤더를 얻는 것이 더 좋고/더 간단합니까? ,

답변

3

나는이 비슷한 문제를 가지고 있었고, 내 경우에는 나는 그러나 "헤더"개체의 모든 필드가 null이며, 나는이 방법을 시도 OperationContestScope

ManagerSoapClient client = new ManagerSoapClient(); 

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) 
{ 
    client.Authenticate(...); // <---- The change 
    var headers = OperationContext.Current.IncomingMessageHeaders; 
} 
+0

내부의 클라이언트 호출을 넣어 그것을 해결 비록 Fiddler에서 Action, To, From 같은 헤더 필드를 볼 수 있습니다. 어떤 아이디어? – AntonK

관련 문제