October 31, 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 »

The Next Step: Using a WorkFlow; Object Model, WebServices

Each WorkFlow needs a Host to reside in, as explained in Part I. For the sample WorkFlow, you can use a Windows application that initializes the process, sets the required properties and, eventually, displays the resultant status.

The FrontEnd of the Windows application allows you to specify the value of the two WorkFlow properties and contains one button to initialize the process (See Figure 3).

Figure 3: Windows FrontEnd Application

Note: Because this is a test program, there are no validations for the input or other kinds of code to prevent failures. The first step is set references to the following: System.Workflow.Activities, System.Workflow.ComponentModel, and System.Workflow.Runtime namespaces, making it possible to work with the Object Model of the WorkFlow Foundation. Additionally, a reference must be set to the newly created WorkFlow assembly that will be initialized and used in the code.

It is not actually necessary, but very convenient, to set a statement for "using System.Workflow.Runtime" (in C#) or "Imports System.Workflow.Runtime" (in Visual Basic) at the beginning of the code-behind file. Following this, a global variable for the Runtime object is declared:

Private WorkflowRuntime _myWfRuntime;

The code for the event handler of the button:

private void btnBegin_Click(object sender, EventArgs e)
{
   //Instance of WorkflowRuntime
   if (_myWfRuntime == null)
   {
      _myWfRuntime = new WorkflowRuntime();
      _myWfRuntime.StartRuntime();
   }

   //Parameters
   Dictionary<STRING, object> WfParameters =
      new Dictionary<STRING, object>();
   WfParameters.Add("AuthorName", txtAuthorName.Text);
   WfParameters.Add("NumberOfWords", txtNumberWords.Text);

   //Run
   WorkflowInstance WfInstance =
      _myWfRuntime.CreateWorkflow(typeof
      (Jupiter_WorkflowConsoleApp.Workflow1), WfParameters);
   WfInstance.Start();
}

The event handler of the button initializes the instance of the global WorkFlow Runtime variable if it does not exist; it also uses a Generic of the type Dictionary to declare and hold the values of the parameters for the WorkFlow. Furthermore, it declares a variable of the type WorkFlowInstance that creates the real WorkFlow in the engine, using the method "CreateWorkflow", with the referred WorkFlow and its initialization values (given by the Generic) as parameters. Finally, the instance uses the method "Start" to begin the WorkFlow process.

Because only one instance of the WorkFlow can be used at a time, it is necessary to release the activated objects. The event "FormClosed" can be used for this purpose:

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
   if (_myWfRuntime != null)
   {
      if (_myWfRuntime.IsStarted)
      {
         _myWfRuntime.StopRuntime();
      }
   }
}
Note: For test purposes, the methods of the WorkFlow have been changed to write the status of the Activities to a text file. The WebService is also modified in the same way. From this site the source code for all components can be downloaded.*

To test the WorkFlow, simply run the assembly of the test program with values for the "Author" and "Number of Words" parameters, and the outcome of the process can be followed in the output log file as shown in Figure 4.

Figure 4: Output of the test program

If the "Number of Words" parameter is between 1000 and 2000, the WorkFlow follows the "Publishing" path, otherwise, the "Rejecting" path will be followed. Finally, the WebService is called with a "Message" parameter, and the parameter is written in the log file.

When the WorkFlow is concluded and tested, Visual Studio allows the developer to create an installation package with different possibilities: publication to a Web Server, to a shared file path, or to a stand-alone installation for distributable media. Additionally, it is possible to make the WorkFlow updatable using the Microsoft "OneClick" deployment technology, which automates the publishing of the application, letting users install the software and minimizing the impact of software rollouts.

A second method of publishing is by creating a WebService of the whole WorkFlow. Visual Studio consents to the construction of Web services from the WorkFlow project using a Wizard. The Wizard takes care of the correct configuration of IIS, creates the appropriate proxys and copies the files to the necessary directories.





Page 2 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel