简体   繁体   English

具有TransferMode = Streamed的WCF REST客户端-记录整个(“原始”)http请求/响应

[英]WCF REST client with TransferMode=Streamed - logging entire (“raw”) http requests/responses

This is on .Net 4, full framework. 这是在.Net 4完整框架上。

I'm trying to make a simple winforms app that will make some simple WCF REST calls. 我正在尝试制作一个简单的winforms应用程序,该应用程序将进行一些简单的WCF REST调用。 It's using ChannelFactory and the service contract interface. 它使用ChannelFactory和服务协定接口。 Of the ~20 methods in the interface, 2 of them involve Stream (an upload and a download method) so the service side (and currently also the client side) using TransferMode=Streamed. 接口中的约20种方法中,有2种涉及Stream(上载和下载方法),因此服务端(当前还包括客户端)使用TransferMode = Streamed。

My goal is to include the full HTTP request and response (much like you would see in ethereal/wireshark, or fiddler, or whatever), with headers, in a textbox of the winforms app (just to show what went over the wire) 我的目标是在Winforms应用程序的文本框中包含完整的HTTP请求和响应(就像您在ethereal / wireshark,fiddler或其他东西中看到的一样)和标头(仅显示通过网络传输的内容)

In trying to use the built-in diagnostics (via SvcConfigEditor) and my own (via implementing IClientMessageInspector and then IEndpointBehavior to add the inspector, then channelFactory.Endpoint.Behaviors.Add to add the behavior :), I'm having 2 issues: 在尝试使用内置诊断程序(通过SvcConfigEditor)和我自己的诊断程序(通过实现IClientMessageInspector然后通过IEndpointBehavior添加检查器,然后使用channelFactory.Endpoint.Behaviors.Add添加行为:)时,我遇到了两个问题:

  1. When doing request.ToString() or reply.ToString() in BeforeSendRequest and AfterReceiveReply, it only gets the 'body' and not the headers. 在BeforeSendRequest和AfterReceiveReply中执行request.ToString()或Reply.ToString()时,它仅获取“ body”,而不获取标头。 Digging around in the objects in the debugger it looks like the reply has them in reply.Properties["httpResponse"], but the request's request.Properties["httpRequest"] has an empty Headers property even though Fiddler shows headers for Content-Type, Host, Accept-Encoding, and Connection. 在调试器中的对象中四处搜寻,看起来回复就像是在回复它们。Properties[“ httpResponse”],但是请求的request.Properties [“ httpRequest”]具有空的Headers属性,即使Fiddler显示了Content-Type的标头,主机,接受编码和连接。 It seems like there's likely a better way to get the 'raw' message that I'm missing (and if there's not, someone probably knows an existing chunk of code to 'reconstruct' the raw one from the Message) 似乎有一种更好的方法来获取我丢失的“原始”消息(如果没有,则有人可能知道现有的代码块可以从“消息”中“重构”原始代码)
  2. Since the transfer mode is Streamed, the 'body' part just shows up as the string '... stream ...', both in SvcTraceViewer (and the 'raw' svclog - even with logEntireMessage=true) and when doing a ToString(). 由于传输模式是流式的,因此在SvcTraceViewer(和“原始” svclog中-即使使用logEntireMessage = true)以及执行ToString时,“正文”部分仅显示为字符串“ ... stream ...”。 ()。 If the mode is Buffered instead, it shows the actual body fine. 如果该模式改为“缓冲”,则会显示实际的车身细度。 I tried making a copy with reply.CreateBufferedCopy(int.MaxValue); 我试图用reply.CreateBufferedCopy(int.MaxValue);进行复制; but that then caused the actual WCF call to fail with an InvalidOperationException: This message cannot support the operation because it has been copied. 但这随后导致实际的WCF调用失败,并显示InvalidOperationException:此消息无法支持该操作,因为已将其复制。

One fallback would be to move the client to Buffered and just change to StreamedRequest for the one upload call and StreamedResponse for the download call (but I'd have to do that programmatically AFAICT, as it's set at the binding level in the config and I don't see anyway of doing it via attributes on the calls), which would take care of the 'body' part and leave me with just the "get the http request headers" (issue #1, specifically request.Properties["httpRequest"].Headers being empty) to deal with, but I'm hoping there's some way of logging the 'raw' messages without doing so, leaving the TransferMode as Streamed. 一个后备方法是将客户端移动到Buffered,然后将一个上载调用更改为StreamedRequest,将一个下载调用更改为StreamedResponse(但我必须以编程方式执行AFAICT,因为它是在config和我的绑定级别中设置的无论如何都看不到通过调用的属性来执行此操作),这会处理“正文”部分,只剩下“获取http请求标头”(问题1,特别是request.Properties [“ httpRequest] “]。标头为空)来处理,但我希望有一些方法可以记录“原始”消息,而无需这样做,而将TransferMode保留为Streamed。

Thanks! 谢谢!

I can't find any reference right now, but it's a known fact that you cannot capture the contents of a streamed message to WCF tracing. 我现在找不到任何参考,但这是众所周知的事实,您无法捕获到WCF跟踪的流式消息的内容。 When streaming is enabled, only the headers of the message will be traced. 启用流传输后,将仅跟踪邮件的标头。

Here's the source: Configuring Message Logging on MSDN 这是源:在MSDN上配置消息记录

See towards the end of the page: 参见页面末尾:

Service Level 服务级别

Messages logged at this layer are about to enter (on receiving) or leave (on sending) user code. 在此层记录的消息即将输入(接收)或离开(发送)用户代码。 If filters have been defined, only messages that match the filters are logged. 如果定义了过滤器,则仅记录与过滤器匹配的消息。 Otherwise, all messages at the service level are logged. 否则,将记录服务级别的所有消息。 Infrastructure messages (transactions, peer channel, and security) are also logged at this level, except for Reliable Messaging messages. 除了“可靠消息传递”消息外,还会在此级别记录基础结构消息(事务,对等通道和安全性)。 On streamed messages, only the headers are logged. 在流式消息上,仅记录头。 In addition, secure messages are logged decrypted at this level. 此外,安全消息在此级别被记录为解密。

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

相关问题 WCF HttpTransport:流式传输与缓冲的TransferMode - WCF HttpTransport: streamed vs buffered TransferMode 从wcf绑定transferMode从“Buffered”更改为“Streamed”是否被认为是客户端的重大变化? - Is changing from wcf binding transferMode from “Buffered” to “Streamed” considered a breaking change for the client? WCF:通过netTcpBinding通过TransferMode = Streamed将文件传输到服务器时,对客户端的响应延迟 - WCF: response to client delayed when transferring file to server with TransferMode=Streamed over netTcpBinding 使用basichttpBinding和流的transfermode的WCF中出现错误请求 - error Bad Request in WCF using basichttpBinding with transfermode of streamed 如何将自定义对象序列化为流(WCF HttpBinding-TransferMode =流式) - How to seralize custom objects into stream (WCF HttpBinding - TransferMode = Streamed) TransferMode = Streamed时发生TimeoutException - TimeoutException when TransferMode=Streamed NetTcpBinding()与TransferMode.Streamed - NetTcpBinding() with TransferMode.Streamed 在MSMQ端点上使用消息检查器记录WCF请求和响应 - Logging WCF Requests and Responses using Message Inspectors on an MSMQ Endpoint WCF REST流式GET响应 - WCF REST Streamed GET response 具有WebHttpBinding /行为的C#WCF HTTP Rest客户端,用于在请求正文中发布RAW数据 - c# WCF HTTP rest client with WebHttpBinding/Behavior for POSTing RAW data in request body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM