As we can see in the previous image, it was necessary to define a new argument of type int, i.e., we defined an InArgument for accepting input arguments when initialized in the WF host. By using InArgument, OutArgument, and InOutArgument, we can flow data into the workflow when it starts and out of the workflow when it ends. But for passing data from the caller into the workflow when it is executing we need to create a new activity. We will create and define this activity as ReadNumber activity. For creating this new ReadNumber activity, we have to create a new code file, for instance named as ReadNumber.cs, with the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
namespace SumIntegers
{
public sealed class ReadNumber : CodeActivity
{
public OutArgument<int> OutNumber { get; set; }
protected override void Execute(CodeActivityContext context)
{
OutNumber.Set(context, Int32.Parse(Console.ReadLine()));
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
namespace SumIntegers
{
public sealed class ReadNumber : CodeActivity
{
public OutArgument<int> OutNumber { get; set; }
protected override void Execute(CodeActivityContext context)
{
OutNumber.Set(context, Int32.Parse(Console.ReadLine()));
}
}
}
We must save and build the project so that we can use this activity in workflow designer. Therefore, in the workflow designer we can drag this new activity to the sequence activity, as we can see in the following image
In the properties of the ReadLine activity, we associate the OutAurgument OutNumber to the InArgument n.
Then, we also drag inside to the Sequence activity the Assign activity, for assigning to a variable the value of the sum. For this, we have to define a new variable sum, as it is in the following image
The variable has a type, in this case Int32, and also a scope, which in this case is the sequence activity scope.
For doing the sum, we may also define a new variable i and a While activity for incrementing the sum variable.
The following image shows all the workflow:
As we can see, we have defined inside the body of the While activity a new Sequence activity, that for each turn, includes two Assign activities for updating variables i and sum. The While activity has also a stopping condition that ensures that it is the sum of the first n numbers. The workflow ends printing to the Console the result of the sum.
Creating the workflow in WF using C# code
For creating a workflow using C# code and using Visual Studio 2010, we start by selecting File -> New Project -> C# section -> Console Application to create a new project. It is necessary to add a reference to the System.Activities assembly, as already mentioned in "A first workflow with WF4".
Change the Program.cs file to:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Statements;
using System.Activities.Expressions;
namespace SumCodeNumbers
{
class Program
{
static void Main(string[] args)
{
WorkflowInvoker.Invoke(new MyActivity());
}
public class MyActivity : Activity
{
public Variable<int> sum = new Variable<int>("sum", 0);
public Variable<int> i = new Variable<int>("i", 0);
public InArgument<int> n { get; set; }
public MyActivity()
{
this.Implementation = () => new Sequence()
{
Variables = { sum, i },
Activities ={
new WriteLine(){
Text=" Insert a number"
},
new ReadNumber(){
OutNumber=new OutArgument<int>(env=> n.Get(env))
},
new While(){
Body = new Sequence(){
Activities ={
new Assign<int>{
To=new OutArgument<int>(sum),
Value = new InArgument<int>(env =>
sum.Get(env)+i.Get(env))
},
new Assign<int>{
To=new OutArgument<int>(i),
Value = new InArgument<int>(env => i.Get(env)+1)
}
}
},
Condition=ExpressionServices.Convert<bool>
(env=> i.Get(env)<=n.Get(env))
},
new WriteLine(){
Text=new InArgument<string>(env=> "The sum is " +
sum.Get(env).ToString())
}
},
};
}
}
}
}
In this example, we have also included in the project the file ReadNumber.cs.






