Workflow 4.0 coded implemented in VS 2010

As I already describe in my previous article (Start working with Workflow 4.0 in Visual Studio 2010), A workflow is a series of distinct programming steps or phases. Each step is modeled in WF as an Activity. It has been completely reengineered from the ground up. It is a way to execute business processes and in today's applications supporting business processes we are frequently using Windows Workflow technologies as the key technical solution. workflow can be implemented in code or with the designer; the choice is simply a matter of preference.

Here I am going to explain workflow implemented step by step using code not designer,

Lets start

Step 1: In workflow coded implemented you doesn't need workflow application. So, start an simple windows console application name WorkFlowCoded

windows console application

Step 2: The auto generated Program.cs class having the below default code, you need to change this code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
 

Step 3: First add a reference to System.Activities. This will enable you to use the workflow activities in your application.

Add reference in workflow
 

Step 4: Add following namespaces

using System;
using System.Activities;
using System.Activities.Statements;
using System.Activities.Expressions;

Step 5: Call the workflow method "CreateWorkflow()" using WorkflowInvoker and write code for exit prompt message, inside the main() function to complete its implementation

    WorkflowInvoker.Invoke(CreateWorkflow());
    Console.WriteLine("Press ENTER to exit");
    Console.ReadLine();

Step 6: Now define the CreateWorkflow() method

    static Activity CreateWorkflow()
    {
        return new Sequence()
        {
            Activities =
        {
        new WriteLine()
        {
            DisplayName = "Hello",
            Text = "Hello, World!"
        }
        }
        };
    }

What happen in above code

We create an CreateWorkflow() method which is declared to return an Activity. It actually returns an anonymous instance of the Sequence class. The sequence activity is derived from the base activity class. So the compiler returns the Sequence instance as its base class, Activity.

You can see the difference between
workflow coded implementation and workflow design implementation. In coded implementation we need to define and as well as call the workflow in single class and in design implementation we just need to call the workflow reference which was defined in its xaml file, like below figure shows

design workflow

workflow xaml

Here is the complete code

using System;
using System.Activities;
using System.Activities.Statements;
using System.Activities.Expressions;
 
namespace WorkFlowCoded
{
    class
Program
    {
        static void Main(string[] args)
        {
            WorkflowInvoker.Invoke(CreateWorkflow());
            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();
        }
        static Activity CreateWorkflow()
        {
            return new Sequence()
            {
                Activities =
            {
            new WriteLine()
            {
                DisplayName = "Hello",
                Text =
"Hello, World!"
            }
            }
            };
        }
    }
}
 

Step 7: Now run the application the output looks like below

workflow output

Thank you for reading the article......


Similar Articles