2010-01-25 1 views
6

나는 서비스 참조를 연결하는 간단한 C# 3.5 .Net 콘솔 응용 프로그램이 있습니다. 모든 것이 잘 작동하고 전화를 걸고 응답을 받지만 지금은 돌아 오는 메시지에서 Soap 헤더를 살펴야한다고 들었습니다.C#의 서비스 참조에 대한 수신 응답에 대한 전체 SOAP 응답 (헤더 포함)을 보려면 어떻게합니까?

.Net WebService Studio는 매우 멋지며 Soap 요청과 Soap 응답을 모두 보여줍니다.

ResponseCode: 200 (OK) 
Content-Language:en-US 
Content-Length:30048 
Content-Type:text/xml; charset=utf-8 
Date:Mon, 25 Jan 2010 19:57:47 GMT 
Server:WebSphere Application Server/6.1 

<?xml version="1.0" encoding="utf-16"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Header /> 
    <soapenv:Body> 

가 어떻게 내 응용 프로그램에서 비슷한 생성 할 수 있습니다 응답을

는,이 같은 뭔가를 보여줍니다?

내가보기에 관심이있는 응답은 WebService Studio를 폭파시키기에 충분한 메시지를 반환하는 다른 방법입니다. 이 도구를 사용하여 메시지 크기 매개 변수를 설정하는 방법을 알지 못합니다. 그래서 저는이 정보를 직접 수집하고 싶습니다.

어떻게 할 수 있습니까?

답변

7

WCF에는 config file을 통한 추적이 있거나 메시지를 직접 기록하기위한 동작을 구현할 수 있습니다.

은 다음과 같이 동작을 추가

Service1SoapClient client = new Service1SoapClient(); 
client.Endpoint.Behaviors.Add(new MessageInspectionBehavior()); 
client.HelloWorld(); 

및 코드 :

class MessageInspectionBehavior : IClientMessageInspector, IEndpointBehavior 
{ 
    public void Validate(ServiceEndpoint endpoint) 
    { 
    } 

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
    { 
    } 

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
    { 
    } 

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
    { 
     clientRuntime.MessageInspectors.Add(this); 
    } 

    public object BeforeSendRequest(ref Message request, IClientChannel channel) 
    { 
     //Write request message 
     Console.WriteLine(request.ToString()); 
     return null; 
    } 

    public void AfterReceiveReply(ref Message reply, object correlationState) 
    { 
     // Write out http headers 
     foreach (var property in reply.Properties) 
     { 
      if (!(property.Value is HttpResponseMessageProperty)) continue; 
      var httpProperties = (HttpResponseMessageProperty)property.Value; 
      foreach (KeyValuePair<object, object> kvp in httpProperties.Headers) 
      { 
       Console.WriteLine(kvp.Key + ":" + kvp.Value); 
      } 
     } 
     // Write result message 
     Console.WriteLine(reply.ToString()); 
    } 
} 

는 마찬가지로 당신이 IDispatchMessageInspector와 IServiceBehavior과 서비스 측면에서 로거를 작성할 수 있습니다.

+0

맵시를! 나는 내일 그 소용돌이를 줄거야! :) 감사! –

+0

응답의 http 헤더를 표시하는 편집이 추가되었습니다. –

+0

누구나 콘솔 대신 문자열로 응답을 받도록 수정하는 방법에 대한 아이디어가 있습니까? –

1

많은 도움이되었습니다. 그러나 앞으로는 using 문이 필요할 수도 있습니다.

다른 사람을 위해, 당신은 포함해야합니다

Using System.ServiceModel; 
Using System.ServiceModel.Dispatcher; 
Using System.ServiceModel.Channels; 
Using System.ServiceModel.Description; 
관련 문제