Is there any way to instruct WCF (through an inspector) to skip the execution of an operation?


  • Hi,

    I have a WCF service and I register a message inspector. In IDispatchMessageInspector.AfterReceiveRequest implementation I would like to implement a caching layer - based on the results of executing an algorithm I conclude that the consumer has already the most up-to-date data and I want to bypass the execution of the operation (which is considered heavy).

    The simplest and most obvious solution is to implement a mechanism for checking a condition in the body of the operation itself and simply return from there if cache conditions are met. I actually have already this mechanism in place. But I want to to hide completely the cache implementation from the operation logic. The situation is that I'll have many services and couple of eligible operations for caching in each service. The programmer needs to pay special attention to check for caching consistency in each such operation - this is quite error prone and could transform in an maintenance nightmare.

    One way I imagined would be to throw a specific exception from IDispatchMessageInspector.AfterReceiveRequest after I conclude that the consumer's data is up-to-date. But this solution has the overhead of an exception and probably more important the reply message will reach IDispatchMessageInspector.BeforeSendReply as a fault, which I'd then need to convert it into a successful message. I find it quite ugly and probably the performance will degrade when converting the fault into a successful message - in this case after all, the whole idea of cached operations is to increase execution time and limit resources consumption.

    Is there any way to avoid the execution of an operation? Not necessarily using a message inspector. Is there another "hook" in WCF architecture that would enable this scenario?

    Florin Neamtu

    Monday, November 02, 2009 1:56 PM


All replies

  • Hi,

    You can try implementing IOperationInvoker. This is the interface that has methods with which the operation will be called.
    Please check this link. The code for this at the botom of this link. This custom invoker will provide a caching functionality.It will check that for a given set of parameters if output and return value exists in cache then it will use the cached values otherwise invoke the method and add results to cache.
    • Proposed as answer by Haripraghash Monday, November 02, 2009 2:34 PM
    Monday, November 02, 2009 2:34 PM
  • Thanks a lot for the hint and link. This looks indeed like what I need. 

    • Marked as answer by Florin Neamtu Tuesday, November 03, 2009 1:37 AM
    Tuesday, November 03, 2009 1:37 AM