问题
Is there a way to get a hook into every call from a client made to a WCF service
- as soon as a client request is noticed at the server side (i.e. before deserialization of the request)
- 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