Windows Workflow Foundation supports two types of workflows: sequential and state machines. Over the next several days, I’m going to talk about building state machine workflows. Sequential workflows are procedural in nature (activities are executed in sequence) so, they should seem familiar to most developers. State machine workflows, on the other hand, are event driven in nature. This opens up a host of new scenarios, but, is a little less familiar.
To start with, I want to create a “Hello World” state machine. In other words, I want to create the smallest workflow possible that can successfully execute. Since state machines are event driven, and since the only built in event activities in WF are Web Service Receive activities, we will host the HelloWorld workflow in ASP.Net, instead of in a console application. Below are the essential steps required to create the HelloWorld state machine workflow. The completed code can be copied and pasted from here.
CREATING THE PROJECT
The best way to start is by creating an empty workflow project named “StateMachine_HelloWorld”. Add, to this project, a State Machine Workflow, with code seperation, named HelloWorld.xoml.
State machine workflows consist of a series of states. Each state (except for the completed state) can receive one or more events. These events can contain activities which change the state. If an event is received, which is not available in the current state, that event is not processed.
Our minimal workflow will contain just the initial and completed states. When it is first created, HelloWorld.xoml contains an initial state. We need to add the completed state. Drag another state onto the workflow. Change its name to “HelloWorldCompletedState”. Then change the workflow properties to set the CompletedState to be this new state. Now, we need to create an event which transitions from the initial state to the completed state.
Drag an event driven activity from the tool box to HelloWorldInitialState. It will be named, by default, eventDriven1. Double click on eventDriven1. This will open up a new display which allows you to describe what happens when this event is received. Within eventDriven1, we need three activities: Web Service Receive (the event itself), Code (to write hello world), and SetState (to change to HelloWorldCompletedState). webServiceReceive1 should have its Activitation property set to True, because this event will start the workflow. setState1 should have its TargetState property set to HelloWorldCompletedState.
Web Service Receive activities are mapped to methods in interfaces. So, create an IHelloWorld interface and give it a single method: void Completed_Event(). In the designer, set the properties of webServiceReceive1 to point to this method. Still in the designer, double click on code1. This will allow you to fill in the body of the procedure that is invoked by the code1 activity. Add System.Diagnostics.Debug.Print(“Hello world”).
In order to generate the ASP.Net code, right click on the StateMachine_HelloWorld project and select “Publish as Web Service…”. This will create a new ASP.Net project. Right click on the .asmx file created in the new project and select “Set as Start Page”. Start the solution with debugging. Acknowledge the message which asks if you want to enable debugging.
The test page for the HelloWorld_WebService will be displayed. Click on Completed Event and then click on Invoke. In the output window in Visual Studio, you should see “Hello world”.
Now that we have a state machine workflow running, we can start to experiment with its features. More to come in a future post.