WF4教程第一弹

本文主要是总结自己学习《microsoft windows workflow foundation 4.0 cookbook》第一章的内容。下面给出本章程序的源代码,下载地址如下:Chapter01.rar。下面简要介绍解决方案Chapter01中所有项目。解决方案中的各个项目如下图所示

clip_image001

1.Creating the first WF program:HelloWorkflow

HelloWorkflow is a Workflow Console Application. The workflow we created in WF Designer is actually an XML file. We can open Workflow1.xaml with an XML editor to check it.

In Visual Studio 2010 we can Right-click on Workflow1.xaml then click “查看代码” open Workflow1.xaml as an XML file.

So far, there are no officially published WF4 Designer add-ins for Visual Studio 2008. We need a copy of Visual Studio 2010 installed on our computer to use WF4 Designer, otherwise we can only create workflows by imperative code or by writing pure XAML files.

HelloWorkflow是一个“工作流控制台应用程序”,我们在工作流设计器中创建的工作流其实就是一个XML文件,在VS2010中我们可以通过以下方法查看工作流的XML文件:右键Workflow1.xaml,选择“查看代码”。到目前为止,只有VS2010有WF4的设计器,VS2008中还没有WF4编辑器的插件,因此如果要在VS2008中开发WF4,那么就只能通过编写纯XAML文档的方式进行。

2.Creating a WF program using C# Code

HelloCodeWorkflow is a Console Application.这个项目是通过纯代码的方式来创建WF4项目。具体见代码,也可以参考书本。

3.Initializing a WF program using InArguments

UseInArgument is a Workflow Console Application.在这个项目中主要是创建了InArgument类型的参数。可以为输入参数传递变量。

4.Creating a WF program using OutArgument

UseOutArgument is a Workflow Console Application.OutArgument是输出参数类型,在工作流中可以作为输出参数用。在宿主应用程序中可以通过特定的方法输出该参数中的内容。

The WorkflowInvoder.Invoke method will return a IDictionary type object.

WorkflowInvoder.Invoke方法会返回IDictionary类型对象。

There is a third type of workflow argument: InOutArgument. It is a binding terminal that represents the flow of data into and out of an activity. In most cases, we can use InOutArgument instead of InArgument and OutArgument. But there are still some differences—for example, we cannot assign a string to InOutArgument, while it is allowed to

assign a string to InArgument directly in the host program.

在大多数情况下,可以用InOutArgument参数类型来代替InArgumentOutArgument但是也有一些例外情况,比如我们可以直接将一个string类型的变量赋值为InArgument类型的参数,但是却不能在宿主程序中直接将string字符串赋值给InOutArgument类型的参数。

5.Creating a WF program using InOutArgument

In this task, we will create a WF program using InOutArgument. This type of argument is used to receive values and is also used to pass values out to the caller (WF host).

InOutArgument变量既可以用来获取值,也可以用来向WF宿主输出值。

UseInOutArgument is a Workflow Console Application.

6.Using Variable in a WF program

UseVariable is Workflow Console Application.

We can use Variable temporarily to store a value when a WF program is running. In this task,we will create a WF program that prints five numbers to the console in a loop. We will use the NumberCounter variable as a number counter.

在WF程序运行期间,我们可以通过变量(Variable来暂时保存一个值。

While we can use arguments to flow data into and out of a workflow, we use Variable to store data in a workflow. Every variable has its scope, and can be accessed by activities within its scope. Variable in WF4 is pretty much like variables in imperative language such as C#.

7.Running a WF program asynchronously

UseWorkflowApplication a Workflow Console Application.

In the previous tasks, we used the WorkflowInvoker.Invoke method to start a workflow instance on the same thread as the main program. It is easy to use; however, in most real

applications, a workflow should run on an independent thread. In this task, we will use

WorkflowApplication to run a workflow instance.

通过使用WorkflowInvoker.Invoke方法来启动一个工作流实例。工作流和宿主程序是在同一个线程上运行。这样用起来虽然方便,当时在大多数实际的应用程序中,都是将宿主程序与工作流程序的线程分开。让工作流在一个独立的线程中运行。

As the workflow thread runs simultaneously with the caller thread, the caller thread may terminate before the workflow thread.To prevent this unexpected program quit,we need to use AutoResetEvent to synchronize caller and workflow thread.

如果工作流线程和调用者线程同时运行,调用者线程可能在工作流线程之前结束 为了防止这种意想不到的程序退出,我们需要通过使用AutoResetEvent来使得 调用者工作流线程异步

8.Customizing a MyReadLine activity with Bookmark

MyReadLineActivity a Workflow Console Application.

By using , we can flow data into the workflow when it starts and out of the workflow when it ends. But how can we pass data from the caller into the workflow when it is executing?—Bookmark will help us to do this. In this task, we will create a MyReadLine activity using a bookmark.

可以通过参数InArgument, OutArgument, and InOutArgument来使得数据在工作流中流动。但是我们如何在工作流执行期间从调用程序向工作流传递数据呢。Bookmark可以帮助我们完成这样的任务。

In WF4, workflow is actually an Activity class. We could see "Workflow" as a conception from a macroeconomic viewpoint, while considering "Activity" as a development concept.

在WF4中,工作流实际上就是一个Activity类。我们可以将工作流理解为宏观上的概念,而活动则是开发上的概念。

9.Converting a WF program instance to XAML

ConvertWFInstanceToXML a Workflow Console Application.

In real applications, we would like to write and test WF programs in imperative code, while storing, running, and transmitting workflow as an XAML string or file. In this task, we will convert a WF program instance to an XAML string.

将工作流程序实例转换成XAML类型的字符串。

Consider the following code line:

 

XamlServices provides services for the common XAML tasks of reading XAML and writing an object graph, or reading an object and writing out an XAML file. This statement reads an ActivityBuilder object and writes XAML to an XamlWriter object.

We use ActivityBuilder as an activity wrapper so that the output XAML is a loadable workflow. In other words, if we save, say, a Sequence activity to an XamlWriter directly, then the output XML workflow will be unloadable for further use.

10.Loading up a WF program from an XAML file

LoadUpWorkflowFromXML a Workflow Console Application.

In this task, we will run a WF program by loading it from an XAML file.

在本实例中,通过加载XAML文件来运行工作流应用程序。

11.Testing a WF program with a unit test framework

UnitTestForWFProgram is a Test Project.

[TestClass] indicates it is a unit test class, whereas [TestMethod] indicates a test method. When the Test Project runs, the test method will be executed automatically.

[TestClass]表示这个一个单元测试类。[TestMethod]表示这是一个测试类。当测试项目运行的时候,测试方法会自动执行。

12.Debugging a WF program

DebugWFProgram a Workflow Console Application.

作者:xwdreamer
欢迎任何形式的转载,但请务必注明出处。
分享到:
原文地址:https://www.cnblogs.com/xwdreamer/p/2297013.html