Hook into WCF service before deserialization of client request and after serialization of response

左心房为你撑大大i 提交于 2019-12-08 02:45:17

问题


Is there a way to get a hook into every call from a client made to a WCF service

  1. as soon as a client request is noticed at the server side (i.e. before deserialization of the request)
  2. after serialization of the response has finished

so that e.g. those points can be added to latency measurements. It's OK if this is specific to net.tcp, which is what we are using.

I guess adding a IDispatchMessageInspector would allow me to hook in before deserialization of the request (albeit after creation of a Message object, and I'd like to hook in even BEFORE that), but not after serialization of the response.


回答1:


To hook in before deserializion of requests, I am wrapping the MessageEncoder (for which I also had to wrap the MessageEncoderFactory, and the MessageEncodingBindingElement)

and then I can hook into the MessageEncoder.ReadMessage() overloads.


To hook in after serialization of the response, I am wrapping the IOutputChannel (for which I also had to wrap the IChannelListener and the TcpTransportBindingElement),

and then I can hook into the IOutputChannel.Send()/IOutputChannelEndSend() overloads.

That's as close to the wire as I got, and close enough for me.




回答2:


If the only thing you require is to capture messages that are being sent/received when a given service is called, you can use the suggestions from the following link:

https://msdn.microsoft.com/en-us/library/ms735120(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/aa702726(v=vs.110).aspx

This will allow you to capture to disk all the messages being sent and received, in Xml.



来源:https://stackoverflow.com/questions/25374917/hook-into-wcf-service-before-deserialization-of-client-request-and-after-seriali

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!