Archive for the ‘Windows Workflow Foundation’ Category

Windows Workflow Foundation (WWF)

Sunday, November 30th, 2008

WWF is a tool for building workflow enabled applications on windows. To do this you will need namespace in Microsoft .NET Framework which is called System.Workflow.
WWF provides a flexible flow model for various types of workflow:

  • Sequential workflow in which one task is executed after another
  • State machine workflow in which external events drive the flow
  • Rule-driven workflow in which a set of rules in combination with the state of data drive the order of processing.
  • To use workflow in your .NET application you need extension for Visual Studio 2005.
    Visual Studio 2005 extensions for .NET Framework 3.0 (Windows Workflow Foundation)

    Also there is 12 hands on lab.
    Community site with samples and other tutorials

    And now an example:
    Open Visual Studio 2005 and select new project, C#, workflow project.

    here is how workflow designer looks like

    at the left, in toolbox there are activities. drag and drop activity to workflow designer.

    We will use Code Activity and If else activity.

    here is the very simple application which demonstrate how to use WWF.
    This small demo is for registering flyghts. As I said it is very simple.
    We have one Dictionary variable where we save information about cities
    and two string variable for departure and arrival.

    	public sealed partial class Workflow1: SequentialWorkflowActivity
    	{
            public Dictionary cities =
                    new Dictionary();
    
            public string FromCity;
            public string ToCity;
    
            public Workflow1()
    		{
    			InitializeComponent();
    
                // Add cities in cities collection
                cities.Add(1, "medina");
                cities.Add(2, "london");
                cities.Add(3, "houston");
                cities.Add(4, "karachi");
                cities.Add(5, "tokyo");
                cities.Add(6, "singapore");
                cities.Add(7, "istanbul");
                cities.Add(8, "seattle");
    		}
    	}
    

    as you can see, in the constructor of this class Workflow1, we add some initial values for cities.

    Now we must say to our application which is true and which is false condition?
    Click on IfElseBranchActivity1 in Workflow designer and view Property. There is Condition property which can be: Code Condition or Declarative Rule condition. We will use Declarative Rule Condition. Enter name, like: FlightExisits. After that we must specify some rule. here is how we do that: click next to the expression and enter this text:
    this.cities.ContainsValue(this.FromCity.ToLower()) == True && this.cities.ContainsValue(this.ToCity.ToLower()) == True

    The same is for IfElseBranchActivity2. We will use Declerative Rule Condition. Set name to “FlightNotExists” and Expression to “!this.cities.ContainsValue(this.FromCity.ToLower()) || !this.cities.ContainsValue(this.ToCity.ToLower())”

    Here is the code which playes at the begining of application work. It belongs to CodeActivity:

            private void codeActivity1_ExecuteCode(object sender, EventArgs e)
            {
                Console.WriteLine("Enter Passenger Name : ");
                string passengerName = Console.ReadLine();
    
                Console.WriteLine("Enter Departure City :");
                this.FromCity = Console.ReadLine();
                Console.WriteLine("Enter Destination City :");
                this.ToCity = Console.ReadLine();
    
            }
    

    and here is code for true condition:

            private void codeActivity2_ExecuteCode(object sender, EventArgs e)
            {
                Console.WriteLine("Flight Booking Confirmed!");
                Console.ReadKey();
            }
    

    and here is code for false condition:

            private void codeActivity3_ExecuteCode(object sender, EventArgs e)
            {
                Console.WriteLine("Sorry, No Such Flight Exists!");
                Console.ReadKey();
            }
    

    at the end, here is autogenerated code for Program.cs

    namespace FirstWFProject
    {
        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(FirstWFProject.Workflow1));
                    instance.Start();
    
                    waitHandle.WaitOne();
                }
            }
        }
    }
    

    WP Theme & Icons by N.Design Studio
    Entries RSS Comments RSS Log in