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 >>
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
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.