WF Tutorial, Part 2: Create a simple Workflow

15. December 2013 Workflow Foundation 15

This tutorial is the second part of Dispatcher Timer Windows Workflow Foundation tutorial series.
<< An Introduction to Windows Workflow Foundation
Create and host Workflow services >>

Download the sample project

In this tutorial I’m going to show you how you can create a very simple Workflow application to ask the user’s name and greets her using C#. Visual Studio 2013 and .NET 4.5 is used in this tutorial.

Task 1: Create a new Workflow Console Application Project.

  • Open Visual Studio
  • Choose File, New, Project and Select Workflow Console Application under Installed, Template, Visual C#, WorkFlow project type.

  • Choose any project name you want and press <OK>
  • The Visual Studio will be creating a Workflow1.xaml file for you. Simply remove the file from the solution explorer.

Task 2: Create a simple Workflow to display a message

As it has been mentioned in the introduction to this WF tutorial series everything in Workflow is an activity. Activities are functional units of a Workflow. They can perform a simple task like displaying massage to the user or more complicated tasks made from combination of different other activities. We are going to use the WriteLine Activity to simply display a message.

  • Right click on the Project; Add, New Item, and select Activity from Installed, Visual C# Items, Workflow Item type.
  • Name the activity HelloLiza.xaml and Press <OK>

  • The workflow designer will be opened.
  • From the Toolbox (Ctrl + Alt + x) drag and drop a WriteLine Activity under Primitives category into the Workflow designer.

    WriteLine activity is used to display a string in a console the same way as Console.WriteLine() method.

    The Text attribute of the WriteLine Activity gets a string or you can enter a C# statement.

  • Select the Text box and enter “Hello Liza”.

Let’s run our application and see how it works. Before build the application we need to modify the Program.cs to run our new Workflow.

  • Double click on Program.cs
  • Modify the code as below
class Program
    static void Main(string[] args)
        Activity workflow1 = new HelloLiza();

Take note that we have changed the Workflow1 initializer class to HelloLiza. For every new Workflow, we are actually creating a class. On the 5th line we are creating an Activity object based on our Workflow. WorkflowInwoker is a simple class which its Invoke
method is used to invoke a Workflow. Why I said simple class because System.Acitivities provides different methods to invoke a workflow. WorkflowInvoker is useful for synchronous execution of workflows. For long-running Workflows or Workflows which use PersistenceStores you need to use WorkflowApplication class.

To execute the Workflow:

  • Build your solution (Ctrl + Shift + B) and run your application (Ctrl + F5)

Task 3: Extend the WorkFlow to accept an input from user

We are going to extend our example and ask the user for her name and to greets her using her name and showing her the current time. To do this we need add Variables to our workflow.

  • Open the HelloLiza.xaml
  • From the Toolbox drag and drop an Assign activity above the WriteLine activity which we have added previously.

    Notice that when you drop that activity WF designer will automatically be creating a SequenceActivity for you.

You can see a small blue exclamation mark on the top-right side of the Assign and Sequence activities. This is the WF’s way to tells you that there is an error in the workflow.

  • Opens the Variables tab at the bottom-left side the Workflow Designer

Initially there is no variable in the list and you cannot even add any. You need to select the activity (the scope) which you want to add the variable to.

  • Select the Sequence activity in the designer.
  • In the Variables tab click on Create variable and type “name” for the Variable Name.
  • In the Variable Type column you can select any type. The default is String which is what we want.

Modify the Assign Activity to accept the user’s input. The Assign part of the activity accepts a variable which will be assigned the value provided on the on the textbox on the right side of the equal sign. Any C# expression can be entered to be assigned to the variable.

  • Type the variable’s name which we’ve just created (i.e. name) in the To textbox

    Notice that by typing the variable name Visual Studio provides you with intellisense same as you are writing a C# code

  • Type the following code in the Expression textbox

Take note that you can modify the attributes of an activity from the Properties window too. Especially when you are writing a pretty long C# code in the Expression text box which is so small in the WF designer. To do so just click on the activity you want to change its attribute and in the properties window click on the Browse button […] of the Value attribute. It will open a textbox which is pretty bigger than the designer so you can enter your C# expression there.

To show the entered name by user let’s modify the WriteLine activity.

  • Enter the following line in the Expression textbox (or alternatively in the Properties window)
"Liza says hi to " + name + ", today is " + DateTime.Now.ToString("D")

You can see we can concatenate a string in the Expression textbox. In the line we are showing a greeting message along with the today’s date.

The last task is adding another WriteLine activity above the Assign activity asking the user to enter her name:

  • Drag and drop a WriteLine activity above the Assign activity
  • Enter the following line in the Expression textbox:
"Hello dear, what is your name?"

The final workflow should be the same as bellow:

  • Build the solution (Ctrl + Shift + B) and run the application (Ctrl + F5)
  • Enter your name and press enter

That’s all for this tutorial.

<< An Introduction to Windows Workflow Foundation

15 thoughts on “WF Tutorial, Part 2: Create a simple Workflow”

  • 1
    Bsidey on May 23, 2014 Reply

    Thanks for this. Very easy to follow. Please do some more advanced workflow tutorials in the near future.

    One thing, my console does not pause when it finishes the script but just closes before I can read it. How do I make it wait for the ‘any’ key?

    • 2
      Daniel Yu on April 11, 2016 Reply

      instead of press”F5″, press “ctrl+F5”

  • 3
    Behnam on July 16, 2014 Reply

    Dear Bsidey
    Sorry for the late reply. When you run your program you can use Ctrl + F5 so the console wouldn’t be close at the end of execution.
    Another option is just add an Assign activity at the end to wait for an input from user.
    And thanks for your comment, I will add more posts on WF soon.

  • 4
    Sammy on July 18, 2014 Reply

    This is a great tutorial, very usefull for anyone who want to start learning WF.
    Thanks a lot for this

  • 5
    Gregory Rowes on August 29, 2014 Reply

    WWF seems like a very immature product, don’t have much graphical features. You definitely have to be a c# developer to do anything. Also ho way to graphically see what happen in the process. there are far superior tools than Windows Workflow. I found this really nice workflow tool called cDevWorkflow, developed on Microsoft technology

  • 6
    guli on October 11, 2014 Reply

    Good Data

  • 7
    Praveen Kumar on December 29, 2014 Reply

    Also you can add the Console.ReadKey();

    namespace Chapter8

    class Program
    static void Main(string[] args)
    Activity workflow1 = new Workflow1();

    after this your consoe window will not close no matter which way you are executing your code.

  • 8
    Monica on June 24, 2015 Reply

    Thank you very much for this informative contents Behnam.
    Its really helpful.

  • 9
    Mani on September 28, 2015 Reply

    thanks for the post man
    but one thing when i run the code it is asking me for the input but after entering the input it is not going for the next step of writeline step it is again showing me the new line(like another console.readline()), when i press the enter then the prompt is closed. Give me solution please

    • 10
      Behnam on September 28, 2015 Reply

      Dear Mani
      If you follow the exact steps you should be able to get the expected result.
      I have updated the post and put the downloadable solution on top of the post. Please download the full project and compare with yours see which step you have missed.
      If you couldn’t found your issue then send me your project so I see where you made mistake.

  • 11
    Aejaaz Mohammed on February 29, 2016 Reply

    Good Work..Keep Posting..Helped Much

    • 12
      Behnam on March 1, 2016 Reply

      Thanks Aejaaz!

  • 13
    Félix-Antoine Boucher on June 11, 2016 Reply

    Really simple! I Really like it! Thanks!

  • 14
    Atif on April 15, 2019 Reply

    I like it, very easy to understand especially for beginners

  • 15
    Atif on April 18, 2019 Reply

    I try to create Workflow service and hosted it using given steps but it’s not working properly, THE ERROR MESSAGE IS BELOW ” A project With an output type of class library cannot be started directly.
    in order to debug this project, add an executable project to this solution with references to the library project. set the executable project as the startup project.” Regards Atif

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.