Working with Arguments in Windows Workflow Foundation


In this Article I will be taking you over step by step process of working on Windows Workflow Foundation with Arguments. Here we are going to build a sample application where i will be explaining each and every process in detail.

Overview on Windows Workflow Foundation



WorkFlow is like Biztalk Orchestration. It is an extensible architecture that allows to provide our own implementation. It can be hosted in any type of application which is running on .NET framework. It can be even hosted as WCF service.

Lets Start Building the Application



Overview



In this example we are going to work majorly with 3 files in the Solution Explorer

DOTNET-Windows-Workflow-Foundation-Argument-40.jpg


1. Student.cs
This is a class file. Here the properties will be declared.
We need to add this class implicitly.

2. Workflow1.xaml
This file will be created by default when you create this project.
To work on this file i am going to split it in two parts
a. Declaring Arguments
b. Designing the flow and functionality

3. Program.cs
From this file we will be invoking the workflow by creating object of
the student class and passing parameters to it



Step-1



File --> New --> Project

A window will pop up with the heading "New Project"

DOTNET_Windows_Workflow_Foundation_Argument_1


Step-2



Under "New Project" window

On the left Pane --> Expand " Installed Templates" and select "WorkFlow" from the list.

On the Middle Pane --> select " workflow Console Application"

Name the project as "WF_With_Arguments"

Windows_Workflow_Foundation_Argument_2


Step-3 : Working with Student.cs class file


Right Click on the project.

Select "Add" --> "New Item" --> select "Class"

Name the class as "student.cs"

Windows_Workflow_Foundation_Argument_3


DOTNET-Windows-Workflow-Foundation-Argument-41.jpg

In this class we will be declaring properties.

Write the following code in the file



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WF_With_Arguments
{
public class Student
{
//Properties
public string Student_Name { get; set; }
public string Student_Department { get; set; }
public string Student_Year { get; set; }

}
}




Note :Once the coding is completed. Please Build the project(Ctrl+Shift+B)

Step-4 : Working on Workflow1.xaml file



Under the Solution explorer open "Workflow1.xaml"

DOTNET_Windows_Workflow_Foundation_Argument_5

Declaring arguments



1. Click on the "Arguments" Tab in the design view of the file

DOTNET_Windows_Workflow_Foundation_Argument_6

2. A window will pop up with four sections
a. Name
b. Direction
c. Argument type
d. Default Value.

Here we will creating two Arguments
a. StudentInfo --> Direction "In"
b. Fees --> Direction "Out"

DOTNET_Windows_Workflow_Foundation_Argument_7

Direction "In" and "Out" meaning Input Parameter and Output Parameter. Just how we use in normal functions.Arguments are intended for passing data in or out of the workflow.

The below is the sample code for explaining what is input and output parameter in a function. We are not going to use the below code any where in this application.

DOTNET_Windows_Workflow_Foundation_Argument_8

3.Creating Fees Argument.
Click on the "Create Argument" and enter the values as follows

DOTNET_Windows_Workflow_Foundation_Argument_11

DOTNET-Windows-Workflow-Foundation-Argument-33.jpg

DOTNET_Windows_Workflow_Foundation_Argument_13


4. Creating StudentInfo Argument

In the same way create StudentInfo Argument also and enter the following values

DOTNET_Windows_Workflow_Foundation_Argument_12

1. While entering the Argument Type for "StudentInfo", you need to select "Browse for Types" from the dropdown

2. You will see a pop up window opened "Browse and Select a .Net Type"


DOTNET_Windows_Workflow_Foundation_Argument_9

Expand --> "Current Project" --> WF_With_Arguments [1.0.0.0] --> WF_With_Arguments --> Select "Student"

DOTNET_Windows_Workflow_Foundation_Argument_10

You are done with creating arguments.
Now will go to the actual design part.

Design Section



Drag and Drop the "Sequence" from the toolbox under "Control Flow" and drop it in the design view

DOTNET_Windows_Workflow_Foundation_Argument_14

DOTNET_Windows_Workflow_Foundation_Argument_14

Drag and Drop the "WriteLine" from the Toolbx under "Primitives" Section into the "Sequence" Section

In the textarea type as "Working with Arguments"

DOTNET_Windows_Workflow_Foundation_Argument_15


DOTNET_Windows_Workflow_Foundation_Argument_16


Drag and Drop "Assign" control from the "Toolbox" under "Primitives" section. This control is used for assigning the value to variables

DOTNET-Windows-Workflow-Foundation-Argument-33.jpg

DOTNET-Windows-Workflow-Foundation-Argument-34.jpg

DOTNET-Windows-Workflow-Foundation-Argument-34.jpg


Drag and Drop the "Switch" control from the Toolbox under "Control Flow" into the sequence control. You will get a pop up window asking for the property type whether it is integer,string or objects which we are going to check. Here in this scenario we are going to check the string. So select the "String"


DOTNET_Windows_Workflow_Foundation_Argument_20

DOTNET-Windows-Workflow-Foundation-Argument-24.jpg

StudentInfo is the Argument that we created in the previous step. Student_Department is the property that we created in the student class (student.cs) file.

DOTNET-Windows-Workflow-Foundation-Argument-25.jpg

Click on the "Add an activity"

DOTNET-Windows-Workflow-Foundation-Argument-26.jpg

Drag and Drop "Assign" control from the "Toolbox" under "Primitives" section into "Switch Control" into "Default" section and
declare Fees=10000


DOTNET-Windows-Workflow-Foundation-Argument-27.jpg

Click on the "Add new case"

DOTNET-Windows-Workflow-Foundation-Argument-28.jpg

In the "Case value" enter the text as ComputerScience"

DOTNET-Windows-Workflow-Foundation-Argument-29.jpg

Drag and Drop "Assign" control from the "Toolbox" under "Primitives" section into "Switch Control" into "ComputerScience" section and
declare Fees=20000

DOTNET-Windows-Workflow-Foundation-Argument-30.jpg

DOTNET-Windows-Workflow-Foundation-Argument-31.jpg

In the same way create another case "Mechanical" , follow the above method to create the "Mechanical" department.

DOTNET-Windows-Workflow-Foundation-Argument-32.jpg

At last we are done with working with "Workflow1.xaml"


Working on the "Program.cs" file



Open the "Program.cs" file . Please write below code



using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using System.Collections.Generic;// need to include this namespace

namespace WF_With_Arguments
{

class Program
{
static void Main(string[] args)
{
//Creating object for the class student and passing values to the properties
Student std = new Student() { Student_Name = "Baskar", Student_Department = "Mechanical", Student_Year = "FIRST" };

//You can uncomment this for testing the other cases
//Student std = new Student() { Student_Name="Baskar",Student_Department="COMPUTER",Student_Year="FIRST" };
//Student std = new Student() { Student_Name = "Baskar", Student_Department = "Meechanical", Student_Year = "FIRST" };


//Dictionary Created.With the input arguments
IDictionary input = new Dictionary
{
{ "StudentInfo" , std }
};

// Code to execute the WF
IDictionary output = WorkflowInvoker.Invoke(new Workflow1(), input);

// Get the TotalAmount returned by the workflow
int student_Fees = (int)output["Fees"];
Console.WriteLine("____________________________");
Console.WriteLine("For the "+std.Student_Department.ToString() + " Department");
Console.WriteLine("The Fees is :{0} ", student_Fees);
Console.WriteLine("____________________________");
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}
}
}




Output



First Output


Student std = new Student() { Student_Name = "Baskar", Student_Department = "Mechanical", Student_Year = "FIRST" };


While passing the above parameter the output will be

DOTNET-Windows-Workflow-Foundation-Argument-35.jpg

Second Output


Student std = new Student() { Student_Name = "Baskar", Student_Department = "ComputerScience", Student_Year = "FIRST" };


While passing the above parameter the output will be

DOTNET-Windows-Workflow-Foundation-Argument-38.jpg


Attaching the project for reference. You can download it.


Attachments

  • DOTNET-Windows-Workflow-Foundation-Argument-Project (44878-225957-DOTNET-Windows-Workflow-Foundation-Argument-Project.zip)
  • Comments

    Author: srirama30 May 2013 Member Level: Gold   Points : 2

    Hi Baskar,

    I am sorry that to say your code/Article does not seem any informative information at all.You just copied the Code from some where and You run it in your system here.And print the screens here.There you had describe some thing with lot of images and arrows.Is there any thing a new or discovered or even invented one.

    Look at my articles or Resources or Code Logics.If you have Find those any where those I will give my all hard earned Money



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: