次の方法で共有


What State is my StateMachine in?

Recently saw this question in the MSDN Workflow Forum

Is there a (SetState) in the new StateMachine Activity?

Many users of Workflow in .NET 3.5 took advantage of the StateMachineWorkflowInstance class to determine what state a workflow is in, and also to force it to a new state.  Some people use this class as a way to enable/disable controls in the UI of an application.

I recall a conversation I had with some developers from a bank that wrote an application for their tellers that used this feature.  One problem they had was that when the UI used StateMachineWorkflowInstance to read the current state of the workflow it locked the instance. 

So here we are in WF4 and many people couldn’t move apps forward from WF3.5 because of missing functionality.  You know I hate that so I set out to solve this problem as quickly as possible which means fixing it with Microsoft.Activities.Extensions.

Unlike WF3, in WF4, any activity can contain a state machine, including other state machines. Therefore any workflow may have multiple state machines, each of which could be in a different state, however only one of them is active at any given time.
The solution is to use a tracking participant and persistence participant to track the current state and possible transitions. There are two classes which you will use to do this.

  • StateMachineStateTracker
  • StateTrackerPersistenceProvider

I’ve written a couple of samples that will show you how to do this with WorkflowApplication and Workflow Services

SNAGHTML8a6a0a 

Comments

  • Anonymous
    June 18, 2012
    Hi Ron,  Can I use the tracking service to implement the folowwing scenario. Our application is a silverlight and WCF application.We have some bussiness processes which drives the UI which is currently part of the client itself.We thought of creating a workflow and deploy it as service and the silverlight can talk to it and make decessions for the UI.I even found a nice sample from you about workflow service and SL. But my issue is the UI team is not looking to run the bussiness process using the workflow service instead they would like to just get answers from the workflow service. Like , tell me all possible transitions from State A or State B.Or can I go back to State A from State C. Then they would handle the UI accordingly. So I am thinking of creating the workflow using State Machines. which would Use the input parameter to move to any state from initial state and then get all possible transitions from that state as send activity. Sorry If I am not clear with my question. I am basically looking to create a stateless single instance workflow and just run the workflow rules and answer the client whether particular transition is possible or not based on the workflow rules. Is it possible?Are we in right track? How can I acheive this. I did lot of research.But I am not finding much about the stateless workflow scenario. Thanks a lot.

  • Anonymous
    June 18, 2012
    So if the workflow doesn't run the process, then the logic to run the process would be in Silverlight correct?  In that case you run the risk that the code that does run the process is out of sync with the workflow definition.  There is no perf gain because you still have to make a WCF call. I suppose this would work but I would argue for putting the process into the workflow instead.

  • Anonymous
    June 27, 2012
    Hi Ron, thanks for your articles and screencasts, are great! I need a WF state machine workflow service, I would like to know how to send a message to the service without calling a specific OperationName, I would like to send a message to the service (it should have multiple receives but just one waiting) and the state machine service itself would know pass the message to the receive that is in wait state, do you have an example? Ron thanks very much

  • Anonymous
    June 27, 2012
    Well in order to receive a message your StateMachine has to have a Receive activity with an operation pending.  So you will need to send a message to an operation.  You could have a generic operation that sends a number or string that you could then use to direct the flow.

  • Anonymous
    July 17, 2012
    Hi Rob, i have the following machine state: (two states and two transitions) [Lock] ----> [Unlock] [       ] <---- [          ] i added "recieve"  activities in the triggers of the transitions. its working fine. (calling Lock and Unlock from client) the problem that when i call operation (lets say lock) on a transition  that is not the current state of the item (lets say lock) i get timeout exception and dont know what really happend. i excepct to have exception like "item is not in this state" or something like that. any idea?

  • Anonymous
    July 17, 2012
    @Wasim - When you send a message to a Workflow Service that is not currently expecting that message you should get a CommunicationException at the client.  Check out my post on Troubleshooting Workflow Services - you need to take a look at the tracking - perhaps it will reveal what is going on.

  • Anonymous
    July 17, 2012
    Hi Ron, am afraid am not getting any exception, if I call operation on worng state, it will wait till the work flow hits the corresponding state and it will run.  i am afraid this not what am looking for. I want to have the call cancelled if i run it on wrong state and even get an exception. Thanks.