I’ve got some WCF services (hosted in IIS 6) which use the Enterprise Library (4.0) Validation Application Block. If a client submits a message which fails validation (i.e. gets thrown back in a ValidationFault exception), I’d quite like to be able to log the message XML somewhere (using code, no IIS logs). All the validation happens before the service implementation code kicks in.
I’m sure it’s possible to set up some class to get run before the service implementation (presumably this is how the Validation Application Block works), but I can’t remember how, or work out exactly what to search for.
Is it possible to create a class and associated configuration that will give me access to either the whole SOAP request message, or at least the message body?
I found a blog post which seems to do what I want – you create a class that implements
IDispatchMessageInspector. In theAfterReceiveRequestmethod, you have access to the whole incoming message, so can log away. This occurs after authentication, so you also have access to the user name – handy for logging. You can create supporting classes that let you assign this behaviour to services via attributes and/or configuration.IDispatchMessageInspectoralso gives you aBeforeSendReplymethod, so you could log (or alter) your response message.Now when customers attempt to literally hand-craft SOAP request messages (not even using some kind of DOM object) to our services, we have easy-to-access proof that they are sending rubbish!