This post gives step by step details on how to build a simple sequential workflow using Code Activity.
It assumes that all the components required to develop workflow apps have been installed. List can be obtained here.
Step 1 : Create an empty workflow project in VS 2005. Empty project helps in understanding the architecture of app better as we build from scratch.
Step 2 : As we will be building a console application, change the output type of the project to 'Console Application' using project properties.
Step 3 : Add a new item to the project- 'Sequential Workflow (with code separation)' where we will specify the workflow implementation. This will add two files to your project Workflow1.xoml and Workflow1.xoml.cs.
Step 4 : Double clicking Workflow1.xoml will open the empty workflow diagram. Drag and drop code activity from workflow toolbox on the workflow.In the property window of the code activity enter the value for ExecuteCode parameter which will generate the method by that name. e.g. Code1.
Step 5 : Add code to the Code1 method. Workflow1.xoml.cs will look like this :
namespace HelloWorld
{
public partial class Workflow1 : SequentialWorkflowActivity
{
private void Code1(object sender, EventArgs e)
{
Console.WriteLine("Hello workflow enabled world !");
}
}
}
Step 6 : Add a new code file to your project with Main method. This file will be used to host the workflow and execute it.
The code in the file should be :
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();};
workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
};
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorld.Workflow1));
instance.Start();
waitHandle.WaitOne();
Console.ReadLine();
}
}
}
}
Step 7 : Build the solution, the output should like like this :
Hello workflow enabled world !
The code in the markup file will be like this :
<SequentialWorkflowActivity x:Class="HelloWorld.Workflow1" x:Name="Workflow1" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
<CodeActivity x:Name="codeActivity1" ExecuteCode="Code1" />
</SequentialWorkflowActivity>
Let me just explain some code now :
Class WorkFlow1 is quite simple and as we developed Sequential Workflow we used SequentialWorkFlowActivity. In main program we created an instance of WorkflowRuntime and using that instance we created the instance of our workflow by passing our workflow type to it. Use start() method to execute the workflow. Workflow runtime by default starts all the workflows async. in a separate thread and thats why we used the AutoResetEvent so that main program doesn't exit before our workflow gets completed.
Other Posts
No comments:
Post a Comment