How to send events to Windows Workflow 4 state machine to advance state?

1.4k views Asked by At

I am considering using Windows Workflow State Machine (.NET 4.5) but need some direction.

I have a WCF service with a state machine for a phone call. Let's say the states are:

  • Initialized
  • Ringing
  • Connected
  • Busy
  • No Answer
  • Disconnected

And the possible transitions are:

  • Call
  • Connect
  • Busy
  • No Answer
  • Disconnect

The service exposes a method that is called with a call id and an event ("call", "connect", "disconnect", etc...). I need to be able to load the particular state machine instance that corresponds to that call id and pass in the event so that it can go to the next state (or not change state if there is no corresponding transition from the current state).

For example, if the state machine is in Ringing state and we get "connect" I want to move the state to Connected, but if we get "busy" we move to Busy state. However, if we get the "disconnect" event the state machine should stay in the Ringing state.

I have done this successfully with Stateless, but I want to try it in Windows Workflow.

The MSDN examples don't really show a state machine being driven from outside events.

1) What is a good way to send a transition to the state machine regardless of what state it's in? 2) What is a good way to load/save a particular state machine instance (i.e. it's state and variables) to/from a database (not necessarily SQL Server) given some kind of ID?

1

There are 1 answers

0
HM Chan On

In your Initialized state's entry event, you can add a Receive activity with CorrelationInitializer and CanCreateInstance = true. For each Transition's Trigger, you can put a Receive activity (with Operation Name "Call", "Connect", "Busy", "No Answer", "Disconnect") correlating with the same Correlation Handle.