How to gracefully stop windows service hosting state machine workflow with receive activity?


  • Hi gents, I have a problem to resolve and would really appreciate any comments or, ideally, a solution:

    We have a StateMachine workflow "Workflow1" hosted as windows service via WorkflowServiceHost. Workflow1 is activated by receiving MSMQ message (receive activity). Workflow1 sorts out messages, redirects it to other queues and creates Workflow2 to process already sorted messages (one instance of Workflow2 per message type). Everything works till the point when the service needs to be stopped - it can not if there are unprocessed messages in the queues.

    So the question is: what is the best way to gracefully stop the service and finalize the process?

    I was thinking about WorkflowControlEndpoint with Cancel command and CancellationScope but for that I need a workflow InstanceId. I can potentially get it using CreationEndpoint but looks like the workflow with Receive activity can only be activated with CanCreateInstance = true property on Receive activity and in this case Create doesn't make any sense. 

    Is there other way to control workflow with receive activity, for ex. to instruct it stop receiving messages, clean up and transit to Final State (for state machine)?

    Thanks in advance,


    Friday, February 24, 2012 5:24 AM