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
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"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"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"
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"Declaring arguments
1. Click on the "Arguments" Tab in the design view of the file
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"
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.
3.Creating Fees Argument.
Click on the "Create Argument" and enter the values as follows
4. Creating StudentInfo Argument
In the same way create StudentInfo Argument also and enter the following values
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"
Expand --> "Current Project" --> WF_With_Arguments [1.0.0.0] --> WF_With_Arguments --> Select "Student"
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
Drag and Drop the "WriteLine" from the Toolbx under "Primitives" Section into the "Sequence" Section
In the textarea type as "Working with Arguments"
Drag and Drop "Assign" control from the "Toolbox" under "Primitives" section. This control is used for assigning the value to variables
Drag and Drop the "Switch
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.
Click on the "Add an activity"
Drag and Drop "Assign" control from the "Toolbox" under "Primitives" section into "Switch Control" into "Default" section and
declare Fees=10000
Click on the "Add new case"
In the "Case value" enter the text as ComputerScience"
Drag and Drop "Assign" control from the "Toolbox" under "Primitives" section into "Switch Control" into "ComputerScience" section and
declare Fees=20000
In the same way create another case "Mechanical" , follow the above method to create the "Mechanical" department.
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
{
{ "StudentInfo" , std }
};
// Code to execute the WF
IDictionary
// 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
Second Output
Student std = new Student() { Student_Name = "Baskar", Student_Department = "ComputerScience", Student_Year = "FIRST" };
While passing the above parameter the output will be
Attaching the project for reference. You can download it.
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