How can I get the exception object inside a IDispatchMessageInspector?

571 Views Asked by At

My WCF application uses an IDispatchMessageInspector for some advanced monitoring - when an error occurs and when no error occurs.

I'm looking for a way to get the exception that occurred in my service when I get to the IDispatchMessageInspector.BeforeSendReply method since I need to perform a specific action based on the exception type.

I'm not looking for modifying\logging my errors as I already do that with a custom IErrorHandler.

I thought about adding the exception to the OperationContext when I'm in the IErrorHandler and simply reading it when I'm back in the IDispatchMessageInspector, but I prefer something built in.

Is there any way I can fetch the exception when I'm in the IDispatchMessageInspector.BeforeSendReply method? Maybe somewhere on the OperationContext?

1

There are 1 best solutions below

1
On

IDispatchMessageInspector interface allows you to see the messages arriving at an endpoint before they have been dispatched as well as the corresponding response message before it is sent back to the client.

A couple of points to be noticed about the BeforeSendReply() method that might be useful:

  • If the operation the message was dispatched to was a One-Way operation, this method will still be invoked by the WCF runtime on your IDispatchMessageInspector implementation when the operation has finished its execution. However, the "reply" argument representing the response message will be null, so be sure to check for that before manipulating the message.
  • You can detect in your implementation if the operation the message was dispatched to threw an exception/fault, by checking the IsFault property of the reply message. This will be even true for one-way operations (even though the client will never see it, the server side dispatcher still correctly notifies the message inspectors about it).