Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » WWF » Getting Started with Workflow Foundation in Windows Vista

Getting Started with Workflow Foundation in Windows Vista

Workflow Foundation is the library used to create workflow based applications in Windows Vista. This article shows you how to write your first application using WWF and Visual Studio 2005.

Author Rank :
Page Views : 10483
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Workflow Foundation (WF) library is one of the Windows Vista suites of libraries and ships as a part of .NET Framework 3.0 SDK, formerly known as WinFX SDK. It allows developers to create Workflow based applications. This article is a basic introduction of Workflow Foundation library and shows how to write your first WWF application using Visual Studio 2005.

What is a Workflow?

Workflow based applications have been around for a while and usually being used in Enterprise development. A workflow is a set of activities represented in declarative languages such as XML. Each activity in a workflow is a set of actions to be taken in respect to some business logic. From a programmer's perspective, an activity is some piece of code that executes some business logic. For example, an IF .. ELSE statement can be an activity.

WWF provides common activities used in applications but you can write your own custom activities if needed.

What is Workflow Foundation (WF)?

In .NET Framework 3.0, the API that is used to create workflow based applications is called Workflow Foundation (WF). The WF provides you a choice to write your workflow solely in XAML (see XAML section on Longhorn Corner to learn more about XAML) or a combination of XAML and a .NET programming language such as C# or Visual Basic. If you write a workflow in an XAML file only, there is no need to compile the code. All functions will be loaded dynamically at run-time. However, if you use a combination of a code-behind file and XAML, then you will have to compile the code.

Prerequisites

I am going to use Visual Studio 2005 to develop Workflow based applications. To write Workflow applications, you must install .NET Framework 3.0 SDK (see downloads section of MSDN) after installing Visual Studio 2005. Workflow Foundation is supported on Windows Vista, Windows XP SP2, and Windows Server 2003 operating systems.

To download and getting started with WWF, read Basic Introduction to Workflow Foundation by Dipal Choksi.

Creating Workflow Based Applications using Visual Studio 2005

To understand the internals of a workflow and workflow based application, let's create an application using Visual Studio 2005 and do some reverse engineering.

Visual Studio 2005 supports workflow based applications. To create a workflow based application, select New Project, Visual C# and select Workflow option from the list. You should see various workflow based templates in the Templates section. See Figure 1.

Figure 1. Selecting a Workflow based template in Visual Studio 2005

Figure 1 shows there are 6 templates to create different workflow applications in Visual Studio 2005 listed as following:

  1. Sequential Workflow Console Application
  2. Workflow Activity Library
  3. State Machine Workflow Library
  4. Sequential Workflow Library
  5. State Machine Console Application
  6. Empty Workflow Project

First option allows us to create a sequential workflow application. Selecting Workflow Activity Library, State Machine Workflow and Sequential Workflow Library options allows us to create activity, state machine and workflow libraries. You may use State Machine Console Application option to create a state machine application and the last option creates an empty workflow project where you will have to start by scratch. I shall discuss all of these projects types and their details in my forthcoming articles.

Selecting OK button in Figure 1 creates a workflow application.

Exposing Visual Studio Code

Creating a workflow is just a matter of generating correct XML. You may create a workflow by hand constructing the XML. Alternatively, you may want to use Visual Studio 2005 designer, which provides drag and drop feature. To create a workflow, you simply need to drop the activities from the Toolbox to the designer and code will be written for you.

After you create a Workflow application (Figure 1), Visual Studio generates the workflow code for you and the default page looks like Figure 2.

 

Figure 2 Default sequential workflow

 

The default workflow in Figure 2 has two activities – Begin and Terminate. Now to create the complete workflow, you simply have to add activities between the Begin and Terminate activities.

 

Understanding Workflow Runtime

 

Before we put our application to work, let's take a look at the project files in the Solution Explorer. You will notice a file Program.cs. See Figure 3.

 


Figure 3. Solution Explorer view of workflow application

 

Figure 5 shows the contents of Program..cs. As you can see from this code, WorkflowRuntime is responsible for creating a Workflow runtime object and StartWorkflow method starts the workflow, which takes the Workflow object as a parameter and StopRumtime method stops the workflow runtime.

 

 

Figure 4. Code-behind of Program.cs

 

As you can see from workflow.Runtime.StartWorkflow(type) line of code, it is actually executing our workflow class which is of type Workflow1. I will discuss WorkflowRuntime and other classes in my forthcoming in-depth articles.

 

So a Workflow based application has two parts - The Workflow class and the caller class. As you see from the above discussion, I had a Workflow.cs class that contains all the activities and related code. The second class involved in the application was Program.cs, which is a caller application, where we had WorkflowRuntime executes the Workflow class. So keep this in mind, we can build our workflows independently of the caller application and any application then call these workflows. This theory will play a vital role when I will be implementing real-world large scale workflow-based components and applications.

Putting Workflow to Work

Now let's have our workflow do something for us. I drop a Code activity from the Toolbox to the designer as you can see in Figure 5.

 


Figure 5. Designer with Code activity

 

By dropping a Code activity to the designer, the designer adds following definition of Code activity to Workflow.Designer.cs class:

 

private Code code1;

 

The designer also adds the following code to the InitializeComponent method, which creates an instance of the System.Workflow.Activities.Code class and sets its ID. In the end, adds the code1 activity to the activities of the workflow.

 

this.code1 = new System.Workflow.Activities.Code();

 

//

// code1

//

this.code1.ID = "code1";

 

this.Activities.Add(this.code1);

 

Now if you double click on code1 activity in the designer, the designer adds the following line of code to the designer class in InitializeComponent method.

 

this.code1.ExecuteCode += new System.EventHandler(this.code1_ExecuteCode);

 

The code1.ExecuteCode event handler will be called when the activity is called for the execution.

 

The designer also adds the following code to the Workflow.cs class. Now whatever code you will write in this event handler, it will be executed when the workflow activity will be called.

 

private void code1_ExecuteCode(object sender, EventArgs e)

{

  

}

 

Let me add the following code to this event handler.

 

private void code1_ExecuteCode(object sender, EventArgs e)

{

   Console.WriteLine("Test");

   Console.Read();

}

 

And run the application. The output looks like Figure 6.

 

 

Figure 6. Output of workflow application

 

Now using the same process, you can add as many sequential activities to the workflow and they will be executed in the sequence.

 

Summary

 

This article was an introduction of Workflow Foundation library available in Windows Vista. In this article, we learned how to write our first simple workflow based application in Widows Vista using Visual Studio 2005. In my forthcoming articles, I will cover other workflow types and more internals of related activities. Stay tuned.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
error by vaibhav On March 2, 2007
just created a WF app. the workflow1.cs[design] gives following error Method 'get_CheckTypes' in type 'Microsoft.Workflow.VsShell.Helpers.VSWorkflowCompilerOptionsService' from assembly 'Microsoft.Workflow.VSDesigner, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation. Hide at Microsoft.Workflow.VSDesigner.VSWorkflowDesignerLoader.Initialize() at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host) at System.ComponentModel.Design.DesignerHost.BeginLoad(DesignerLoader loader)
Reply | Email | Modify 
the same problem by Salah On August 5, 2007
i installed framework 3 then installed the extension for VS 2005 but when starting new workflow project i havethe following error: One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes. Method 'get_CheckTypes' in type 'Microsoft.Workflow.VsShell.Helpers.VSWorkflowCompilerOptionsService' from assembly 'Microsoft.Workflow.VSDesigner, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation. Hide at Microsoft.Workflow.VSDesigner.VSWorkflowDesignerLoader.Initialize() at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host) at System.ComponentModel.Design.DesignerHost.BeginLoad(DesignerLoader loader)
Reply | Email | Modify 
Re: the same problem by Mahesh On August 8, 2007

Not sure. You want to make sure the installation order is same as on MSDN downloads page.

Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.