Answered by:
Injecting Behavior

Question
-
I want to be able to execute cleanup code after my message has been sent through WCF. This doesn't have specific business logic and I might want to move which WCF clients execute this code. Is there a hook in WCF where I can just add a class in a .config file and the runtime will execute the code after the message is sent?
Thanks
Tuesday, June 8, 2010 10:12 PM
Answers
-
You can intercept messages by implementing IClientMessageInspector.
http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector.aspx
The MSDN documentation page i linked has an example of how to implement the IClientMessageInspector interface, and how to implement an IEndpointBehavior to tell WCF to use your inspector, and how to change .config file to make your client use the endpoint behavior.
- Proposed as answer by Steven Cheng - MSFT Tuesday, June 15, 2010 6:51 AM
- Marked as answer by Mog Liang Tuesday, June 15, 2010 6:51 AM
Tuesday, June 8, 2010 10:52 PM -
I agree with Sherif that the MessageInspector is a good extension point for your scenario. MessageInspector can help hook the underlying message of each WCF service operation (you can define a custom MessageInspector to do pre/post processing on the underlying Message object).
You can either programmtically inject MessageInspector or using configuration file(through endponit or contract behavior).
#Message Inspectors
http://msdn.microsoft.com/en-us/library/aa717047.aspx#Writing a WCF Message Inspector
http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Mog Liang Tuesday, June 15, 2010 6:52 AM
Thursday, June 10, 2010 3:22 AM
All replies
-
You can intercept messages by implementing IClientMessageInspector.
http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector.aspx
The MSDN documentation page i linked has an example of how to implement the IClientMessageInspector interface, and how to implement an IEndpointBehavior to tell WCF to use your inspector, and how to change .config file to make your client use the endpoint behavior.
- Proposed as answer by Steven Cheng - MSFT Tuesday, June 15, 2010 6:51 AM
- Marked as answer by Mog Liang Tuesday, June 15, 2010 6:51 AM
Tuesday, June 8, 2010 10:52 PM -
I agree with Sherif that the MessageInspector is a good extension point for your scenario. MessageInspector can help hook the underlying message of each WCF service operation (you can define a custom MessageInspector to do pre/post processing on the underlying Message object).
You can either programmtically inject MessageInspector or using configuration file(through endponit or contract behavior).
#Message Inspectors
http://msdn.microsoft.com/en-us/library/aa717047.aspx#Writing a WCF Message Inspector
http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Mog Liang Tuesday, June 15, 2010 6:52 AM
Thursday, June 10, 2010 3:22 AM