简体   繁体   English

WCF - 检查正在发送/接收的消息?

[英]WCF - Inspect the messages being sent/received?

I have 2 solutions: - Server Solution - Client Solution我有 2 个解决方案: - 服务器解决方案 - 客户端解决方案

The server registers itself to my localhost IIS: http://localhost/MyApp/服务器将自己注册到我的本地主机 IIS: http://localhost/MyApp/

The client adds WCF Services (Service References) from the localhost application: http://localhost/MyApp/MyService.svc客户端从 localhost 应用程序中添加 WCF 服务(服务参考): http://localhost/MyApp/MyService.svc

When I'm running the client I want to be able to see the messages being passed back and forth.当我运行客户端时,我希望能够看到来回传递的消息。 I downloaded Fiddler, but it doesn't seem to want to show me any traffic being sent unless I actually use a web browser.我下载了 Fiddler,但它似乎不想显示任何正在发送的流量,除非我实际使用 web 浏览器。 Am I using Fiddler wrong or is there another tool I should be using for this?我使用 Fiddler 是错误的还是我应该为此使用其他工具?


To clarify, what I'm looking to do is to see the actual messages being passed in. I don't want to do anything with them except see them visually with my own eyes.澄清一下,我要做的是查看传递的实际消息。我不想对它们做任何事情,除了用我自己的眼睛直观地看到它们。

I like the WCF Service Log Utility, but I don't think I have the correct setting on there.我喜欢 WCF 服务日志实用程序,但我认为我的设置不正确。 I can't see the actual soap message, just that a message was received.我看不到实际的 soap 消息,只是收到了一条消息。

And also to clarify further, I don't care what tool I use as long as I can easily see the messages themselves.而且为了进一步澄清,我不在乎我使用什么工具,只要我可以很容易地看到消息本身。

To view the message contents you must add a source for System.ServiceModel.MessageLogging in your configuration file.要查看消息内容,您必须在配置文件中添加 System.ServiceModel.MessageLogging 的源。 The message tab in the Trace Viewer will show the full message for a particular service call. Trace Viewer 中的消息选项卡将显示特定服务调用的完整消息。

Here is a sample configuration file:这是一个示例配置文件:

<configuration>

...

   <system.diagnostics>
      <sources>
         <source name="System.ServiceModel"
                      switchValue="All"
                      propagateActivity="true">
            <listeners>
               <add name="traceListener" />
            </listeners>
         </source>
         <source name="System.ServiceModel.MessageLogging"
                      switchValue="All">
            <listeners>
               <add name="traceListener" />
            </listeners>
         </source>
      </sources>
      <sharedListeners>
         <add name="traceListener"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="c:\Traces.svclog" />
      </sharedListeners>
   </system.diagnostics>

   <system.serviceModel>
   <diagnostics>
      <messageLogging logEntireMessage="true"
                                  logMalformedMessages="true"
                                  logMessagesAtServiceLevel="true"
                                  logMessagesAtTransportLevel="true"
                                  maxMessagesToLog="500"/>
   </diagnostics>

...

</system.serviceModel>

...

</configuration>

See the Configuring Tracing topic on MSDN for more information.有关详细信息,请参阅 MSDN 上的配置跟踪主题。 http://msdn.microsoft.com/en-us/library/ms733025.aspx http://msdn.microsoft.com/en-us/library/ms733025.aspx

Maybe I'm missing something, but... Why don't you use the WCF tracing features?也许我遗漏了一些东西,但是......你为什么不使用 WCF 跟踪功能? It's a fantastic troubleshooting tool.这是一个很棒的故障排除工具。 I've used it for services hosted in IIS/WAS also.我也将它用于 IIS/WAS 中托管的服务。

Enabling WCF Tracing启用 WCF 跟踪

BTW, some people don't know it, but you can open traces from server side and client side at the same time, and the Viewer will show you the correlation between the server and client actions in a nice graph.顺便说一句,有些人不知道,但是您可以同时从服务器端和客户端打开跟踪,查看器会以漂亮的图表向您显示服务器和客户端操作之间的相关性。

EDIT: whenever I had to capture TCP/IP traffic, I use WireShark .编辑:每当我必须捕获 TCP/IP 流量时,我都会使用WireShark If you need to do it programatically you can use SharpPCAP , so I can take actions upon what I capture from the network.如果您需要以编程方式执行此操作,您可以使用SharpPCAP ,这样我就可以对从网络捕获的内容采取行动。 But for troubleshooting, much much better to rely on WCF Tracing.但是对于故障排除,最好依靠 WCF Tracing。

If you want to inspect messages programattically, you can implement an IClientMessageInspector interface and register it with your client.如果您想以编程方式检查消息,您可以实现IClientMessageInspector接口并将其注册到您的客户端。

This allows you access to all of the messages, no matter what binding you are using, whereas using tools like Fiddler will only allow you to inspect messages using the HTTP transport channel.这允许您访问所有消息,无论您使用什么绑定,而使用像 Fiddler 这样的工具将只允许您使用 HTTP 传输通道检查消息。

Note, that using this technique, you can do much actually modify the messages or do much more (fire off notifications, for example).请注意,使用这种技术,您实际上可以做很多事情来修改消息或做更多事情(例如,触发通知)。 If all you wish to do is put your eyes on the message, then using tracing might be an easier approach for you.如果您只想关注消息,那么使用跟踪对您来说可能是一种更简单的方法。

Is your service SOAP or RESTful?您的服务是 SOAP 还是 RESTful? You can use the WCF Service Trace Viewer Tool to view the SOAP message headers and bodies.您可以使用WCF 服务跟踪查看器工具查看 SOAP 消息头和正文。 Instructions for configuring your web service for tracing are here .此处为配置 web 服务以进行跟踪的说明

In WCF we can use another way to see actual SOAP messages - custom MessageEncoder - a low-level pipeline extensibility point.在 WCF 中,我们可以使用另一种方式查看实际的 SOAP 消息 - 自定义 MessageEncoder - 低级管道扩展点。 Unlike message inspectors (IDispatchMessageInspector / IClientMessageInspector) it sees original byte content including any malformed XML data.与消息检查器 (IDispatchMessageInspector / IClientMessageInspector) 不同,它会看到原始字节内容,包括任何格式错误的 XML 数据。 You need to wrap a standard textMessageEncoding as custom binding element and adjust config file to use that custom binding.您需要将标准textMessageEncoding包装为自定义绑定元素并调整配置文件以使用该自定义绑定。

Also you can see as example how I did it in my project - wrapping textMessageEncoding, logging encoder , custom binding element and config .您还可以看到我在项目中是如何做到的—— 包装textMessageEncoding、日志编码器、自定义绑定元素配置

Look at this StackOverflow thread: How to use Fiddler to monitor WCF service看看这个 StackOverflow 线程: How to use Fiddler to monitor WCF service

It answers some of your questions.它回答了你的一些问题。 you can also use something like WireShark if you want to examine everything on the wire rather than set up a Proxy, as Fiddler does.如果您想检查线路上的所有内容而不是像 Fiddler 那样设置代理,您也可以使用 WireShark 之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM