appfabricworkflowservice

WF service 4.5 correlation: Either create new instance OR retrieve existing


Is it possible to define a correlation (in a WF 4.5 service) in a such a way that the first operation (a Receive activity) in the WF service EITHER creates a new workflow instance (if the correlation criteria hasn't been received yet) OR retrieves an existing workflow instance (when the correlation criteria has already been received earlier) ?

When would the above be useful?

When there's an "EnqueueItem" operation that groups items based on some properties of each item (correlation criteria) and must create a NEW group each time that a new value-combination (correlation criteria) of the item properties is received.


Solution

  • I didn't find a way to define the operation in such a way (as described in the original question), so I implemented it differently.

    Instead of having one operation that either starts new or retrieves existing, there are two operations, one being the 'EnqueueItem' (can be called many times until a criteria is met) and the other 'StartGroup' (can be called only once to start the WF instance).

    When the client attempts to enqueue an item, it catches an expected InstanceNotFoundException (if the group has not yet been created) and calls 'StartGroup' in this case.