2014-04-02 26 views

回答

0

如果您使用的是WCF,則應該能夠將自定義終結點行爲添加到客戶端上的app.config文件中。

創建自定義消息檢查(例如這裏只記錄到輸出窗口或跟蹤):

public class OutputMessageInspector : IClientMessageInspector 
{ 
    public object BeforeSendRequest(ref Message request, IClientChannel channel) 
    { 
     Debug.WriteLine("Request XML: "); 
     Debug.WriteLine(request.ToString() ?? "<NULL>"); 
     return null; 
    } 

    public void AfterReceiveReply(ref Message reply, object correlationState) 
    { 
     Debug.WriteLine("Response XML: "); 
     Debug.WriteLine(reply.ToString() ?? "<NULL>"); 
    } 
} 

這消息檢查添加到自定義端點行爲:

public class CustomMessageInspectorBehavior : IEndpointBehavior 
{ 
    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
    { 
     clientRuntime.MessageInspectors.Add(new OutputMessageInspector()); 
    } 

    public void Validate(ServiceEndpoint endpoint) { } 

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

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

現在只需更新您的app.config WCF配置將CustomMessageInspectorBehavior添加爲端點行爲。 See MSDN