代码之家  ›  专栏  ›  技术社区  ›  My Other Me

如何记录代理类发送到web服务的XML?

  •  0
  • My Other Me  · 技术社区  · 14 年前

    我从WSDL URL生成了一个代理类。我可以向它发送请求,但我需要记录发送的XML。我该怎么做呢?

    3 回复  |  直到 14 年前
        1
  •  1
  •   sheikhjabootie    14 年前

    我已经在服务器端实现了IDispatchMessageInspector,如前所述 here . 但是,我相信通过实现 IClientMessageInspector 接口。

    如果我没记错的话 Message .ToString()方法返回可以直接放入日志中的SOAP。

    但是,有一个问题我 相信 只是服务器端的,但我会在这里提到它,因为它可能不是。

    消息类设计为只读取一次。因为您是截取消息来记录它,所以需要确保避免将其标记为已读作为副作用。你必须像这样克隆它:

    public class ClientMessageLogger : IClientMessageInspector
    {
        public void AfterReceiveReply(
            ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            // Do nothing.
        }
    
        public object BeforeSendRequest(
            ref System.ServiceModel.Channels.Message request, IClientChannel channel)
        {
            // Create a buffer.
            MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
    
            // Set the request reference to an unspoiled clone.
            request = buffer.CreateMessage();
    
            // Make another unspoiled clone to process (taint) locally within this method.
            Message originalMessage = buffer.CreateMessage();
    
            // Log the SOAP xml.
            Log(originalMessage.ToString());
    
            return null;
        }
    }
    
        2
  •  1
  •   Mark Avenius    14 年前

    如果不使用WCF,还可以打开SOAP日志记录:

    http://msdn.microsoft.com/en-us/library/esw638yk(VS.71).aspx

        3
  •  0
  •   Chris Dickson    14 年前

    你不需要自己编写代码来完成这个任务:只要 configure WCF message logging