代码之家  ›  专栏  ›  技术社区  ›  JohnIdol

如何拦截来自WCF客户端的原始SOAP请求/响应(数据)

  •  9
  • JohnIdol  · 技术社区  · 14 年前

    This question 似乎与我正在寻找的非常接近——我可以设置跟踪,并查看调用服务的日志条目。

    但是,我需要看到原始SOAP请求,其中包含我发送给服务的数据,并且我看不到从svctraceviewer执行此操作的方法(只显示日志条目,但没有发送给服务的数据)-我是否只是缺少配置?

    以下是我在web.config中得到的:

      <system.diagnostics>
        <sources>
          <source name="System.ServiceModel"
                  switchValue="Verbose"
                  propagateActivity="true">
            <listeners>
              <add name="sdt"
                  type="System.Diagnostics.XmlWriterTraceListener"
                  initializeData="App_Data/Logs/WCFTrace.svclog"  />
            </listeners>
          </source>
        </sources>
      </system.diagnostics>
    

    感谢您的帮助!

    更新 :这是我在跟踪中看到的全部内容:

    <E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
      <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <EventID>262163</EventID>
        <Type>3</Type>
        <SubType Name="Information">0</SubType>
        <Level>8</Level>
        <TimeCreated SystemTime="2010-05-10T13:10:46.6713553Z" />
        <Source Name="System.ServiceModel" />
        <Correlation ActivityID="{00000000-0000-0000-1501-0080000000f6}" />
        <Execution ProcessName="w3wp" ProcessID="3492" ThreadID="23" />
        <Channel />
        <Computer>MY_COMPUTER_NAME</Computer>
      </System>
    <ApplicationData>
      <TraceData>
        <DataItem>
          <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information">
            <TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Channels.MessageSent.aspx</TraceIdentifier>
              <Description>Sent a message over a channel.</Description>
                <AppDomain>MY_DOMAIN</AppDomain>
                <Source>System.ServiceModel.Channels.HttpOutput+WebRequestHttpOutput/50416815</Source>
                <ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/MessageTraceRecord">
                <MessageProperties>
                  <Encoder>text/xml; charset=utf-8</Encoder>
                  <AllowOutputBatching>False</AllowOutputBatching>
                  <Via>http://xxx.xx.xxx.xxx:9080/MyWebService/myService</Via>
                </MessageProperties>
              <MessageHeaders></MessageHeaders>
            </ExtendedData>
          </TraceRecord>
        </DataItem>
      </TraceData>
    </ApplicationData>
    
    3 回复  |  直到 6 年前
        1
  •  11
  •   marc_s HarisH Sharma    14 年前

    您没有只显示SOAP消息的特定选项卡,但是XML选项卡包含了整个SOAP消息,不是吗??

    alt text http://i39.tinypic.com/j67rf7.jpg

    这里的XML片段中缺少什么??

    更新: 约翰,不幸的是你没有表现出 <system.serviceModel>/<diagnostics> 部分看起来像-我用于此结果的部分看起来像这样:

    <diagnostics>
      <messageLogging 
          logMessagesAtTransportLevel="true" 
          logMessagesAtServiceLevel="false"
          logMalformedMessages="true" 
          logEntireMessage="true"
          maxSizeOfMessageToLog="65535000" 
          maxMessagesToLog="500" />
    </diagnostics>
    

    你有相同的设置吗?也许你失踪了 logEntireMessage 或者别的什么??

        2
  •  11
  •   Alexander Glen Thomas    11 年前

    我最近遇到了与原始问题更新相同的问题:跟踪显示消息已经发送,但消息本身不在那里。对于我来说,修复方法是添加System.ServiceModel.MessageLogging源。以下是我的system.diagnostics配置部分:

    <system.diagnostics>
       <sources>
          <source name="System.ServiceModel" 
                  switchValue="Information, ActivityTracing" 
                  propagateActivity="true" >
             <listeners>
                <add name="xml"/>
             </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging">
             <listeners>
                <add name="xml"/>
             </listeners>
          </source>
       </sources>
       <sharedListeners>
          <add name="xml" 
               type="System.Diagnostics.XmlWriterTraceListener" 
               initializeData="C:\logfiles\Traces.svclog" />
       </sharedListeners>
    </system.diagnostics>
    

    以及我的System.ServiceModel/Diagnostics部分:

    <diagnostics>
       <messageLogging
          logMessagesAtTransportLevel="true"
          logMessagesAtServiceLevel="true"
          logMalformedMessages="true"
          logEntireMessage="true"
          maxSizeOfMessageToLog="65535000"
          maxMessagesToLog="500" />
    </diagnostics>
    

    添加了messagelogging源之后,traceviewer显示了包含实际SOAP消息的“消息日志跟踪”跟踪。

        3
  •  0
  •   Adam    6 年前

    还有一种查看XML SOAP的方法- custom MessageEncoder . 与IDispatchMessageInspector/IClientMessageInspector的主要区别在于,它在较低的级别上工作,因此它捕获原始字节内容,包括任何格式错误的XML。

    为了使用这种方法实现跟踪,您需要包装一个标准 textMessageEncoding 具有 custom message encoder 作为新 binding element 并将该自定义绑定应用于 config .

    你也可以看到我在我的项目中是如何做到的。- wrapping 文本消息编码,日志记录 encoder ,自定义绑定 element config .