October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building a Practical Application with Windows WorkFlow Foundation

  • August 21, 2006
  • By Gustavo Velez
  • Send Email »
  • More Articles »

In the previous article I provided a theoretical introduction to Microsoft Windows WorkFlow Foundation; in this part the practical applications will be sketched out.

Windows WorkFlow Foundation is one of the core components of the WinFX .NET Framework wave from Microsoft. It provides a singular engine to ensure the workflow execution for all Microsoft applications in the Windows platform. The Foundation consists of a set of tools to enable programmers and users to create workflows; the working engine and the Object Model that permit developers to interact programmatically with the system.

The First Step: Programming a WorkFlow; a Basic Approach to the Code-Behind

Because Visual Studio is Microsoft's default developing tool for all the Windows products, it is also the most powerful tool to develop WorkFlows. After installing the WorkFlow Foundation runtime in the developing machine, it is necessary to install the "2005 Extensions for Windows WorkFlow Foundation" (the software can be downloaded from the Microsoft .NET FrameWork 3.0 site, formerly WinFX; at the time of writing, in Beta 2.2). The software includes project templates, intellisense support for the WorkFlow namespace (System.Workflow) and integrated documentation.

Developing a WorkFlow in Visual Studio is equally effortless as developing an aspx or Windows application: select Activities from the provided Base Activity Library (BAL) that includes a number of fundamental activities, connect them, and write the specified code. This works in the same way as selecting and adding Web or Windows controls. Visual Studio 2005 provides a graphic tool (the WorkFlow Designer) that allows you to define graphically the shape of the WorkFlow, the participant activities and the order of execution. Figure 1 shows a Sequential WorkFlow in the Visual Studio 2005 Designer (central panel), the Tools Panel with the Activities Library (left panel) and the project and properties panels (on the right side).



Click here for a larger image.

Figure 1: The Visual Studio Designer

A typical WorkFlow paradigm begins with an initialization situation, which in this case is a decisions branch (IfElseBranchActivity). The relatively simple example WorkFlow follows an approval process of a document to be published. The initial condition determines if the document concerned pursues a Refusal or a Publishing process (Editorial_Work/Publication_Queue/Publication) and finally, the conclusion of the WorkFlow is a notification to the author.

The example uses various "Code Activities" (Editorial_Work, Publication, and Refusal), an "ifElseBranchActivity" (Approvation), a "Delay Activity" (Publication_Queue), and one "InvokeWebService Activity" (Author_Notification)

The WorkFlow defines two properties at the beginning of the code:

private string_authorName;
public string AuthorName
{
   get { return _authorName; }
   set { _authorName = value; }
}

private int _numberOfWords;
public int NumberOfWords
{
   get { return _numberOfWords; }
   set { _numberOfWords = value; }
}

These properties will be the input parameters that define the principal characteristics of the document to be published.

The ifElseBranchActivity and the Refusal Activities show a red marker at the right corner to indicate that the Activities are not properly configured. In the case of the Refusal Activity, IntelliSense indicates that the "Property 'execution code' is not set"; this property can be changed in the Properties panel. In the code-behind, events are defined for the Code Activities:

private string _authorName;
private void EditorialWork_Handler(object sender, EventArgs e)
{
   //This method does Editorial Work in the document,
   // for example, converts a Word document to HTML
}

private void Publication_Handler(object sender, EventArgs e)
{
   //This method can send the document to the printer queue
   // or publish it in a Content Manager System
}

private void Refusal_Handler(object sender, EventArgs e)
{
   //This method can send the document back to the author or
   // start a back-end process for Administration Work
}

The Code Completion capabilities of Visual Studio 2005 allow you to choose the method to define the property from the callable methods.

The ifElseBranchActivity needs a definition for the condition (therefore the exclamation red mark). The Designer offers a special window to define the condition where IntelliSense and Code Completion are activated. This is shown in Figure 2.



Click here for a larger image.

Figure 2: The Conditions Editor

It also is possible to define a method in the code-behind (that returns true or false) to be connected to the condition branch. In this case, first it is necessary to define the method in code, and thereafter the Code Completion will indicate the method to be selected.

The Delay Activity defines a predetermined waiting period before the WorkFlow resumes the execution. The Delay Activity can be used to await a response from external processes or people. If the delay is going to be for a long time, the runtime engine can "Dehydrate" the WorkFlow (the idle WorkFlow instance will be unloaded from memory and written to a persistent store), and "Rehydrate" it (load the WorkFlow back from the store and assign a thread to be executed on) after the expiration time. This mechanism allows a WorkFlow instance to be idle for a long period without using server resources and insures that the system survives machine reboots.

Finally, the Author_Notification is an "InvokeWebService Activity" that makes a call to an external "SendEmailWebService" (a proxy in the example). This Activity can realize any kind of work, for example—and as the name indicates—sending an e-mail using SMTP or Exchange.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel