2

我正在记录来自我的应用程序的传出 WCF Soap 请求,并发现我正在记录的内容与实际通过线路发送到服务器的内容之间存在差异。

这是我正在记录的内容:

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
 <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/myinterface</Action>
 </s:Header>
 <s:Body>
    <myinterface xmlns="http://tempuri.org/">
    ...

以下是实际输出的内容(使用 WireShark 捕获):

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
 <s:Body>
   <myinterface xmlns="http://tempuri.org/">
   ...

您会注意到 WireShark 捕获没有 Header 或 Action 元素。

我只是注意到这一点,因为我尝试通过 SoapUI 推送我记录的请求。

我正在使用此代码记录请求:

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

和:

public class WCFMessageInspector : IClientMessageInspector
{ 
    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
    {
        Log(request.ToString());

        return null;
    }

和:

    client.Endpoint.Address = new System.ServiceModel.EndpointAddress(address);
    client.Endpoint.Behaviors.Add(new InspectorBehavior());

有谁知道是否有一种方法可以准确地捕捉到正在发生的事情(逐字记录),而无需如上所示的任何后处理?

4

2 回答 2

0

您可以在传输级别配置WCF Message Logging日志App.config消息。

这应该尽可能晚地捕获消息:

对于传出的消息,记录会在消息离开用户代码之后立即发生,并且在消息传输到网络之前立即发生。

<system.diagnostics>
  <sources>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
                 <add name="messages"
                      type="System.Diagnostics.XmlWriterTraceListener"
                      initializeData="c:\logs\messages.svclog" />
        </listeners>
      </source>
    </sources>
</system.diagnostics>

<system.serviceModel>
  <diagnostics>
        <messageLogging 
         logEntireMessage="true" 
         logMalformedMessages="true"
         logMessagesAtServiceLevel="false" 
         logMessagesAtTransportLevel="true"
         maxMessagesToLog="1000"
         maxSizeOfMessageToLog="1000000"/>
  </diagnostics>
</system.serviceModel>
于 2015-07-13T12:46:33.820 回答
0

您可以通过自定义 MessageEncoder捕获确切的内容,与消息检查器 (IDispatchMessageInspector / IClientMessageInspector) 不同,它会看到原始字节内容,包括任何格式错误的 XML 数据。

但是,实施这种方法有点困难。您必须将标准textMessageEncoding包装为自定义绑定元素并调整配置文件以使用该自定义绑定。

您还可以看到我在项目中是如何做到的——包装textMessageEncoding、日志编码器、自定义绑定元素配置

于 2018-07-01T12:35:00.627 回答