大学时,因为毕业论文的关系,开始接触workflow,后来断断续续的关注着这个的发展。前几天dudu的一篇WWF(Windows
Workflow Foundation)的简介[翻译] 又勾起了我对workflow的兴趣。看到园子里有这么多关注wwf的朋友们,又发现网上鲜有关于wwf的中文资料,决定挤出点时间把wwf的doc给整个翻译过来,给园子里跟我一样洋文不怎么行的兄弟们点便利。
小弟才疏,又是第一次写blog,大家见谅了~。为尽量保持原意,我就有什么翻译什么了……那就开始吧~
Windows Workflow Foundation之旅 (一) - 概述
翻译自--ms-help://MS.WinWF.v1.EN/WinWF_GettingStarted/html/55da2060-f10f-4ef4-a923-b38e4504516b.htm
组织并执行一系列的操作或者活动的最自然的方式——那就是工作流——同时也是构造一个工作流程的可执行表现形式的最佳途径。
Windows Workflow Foundation(以下简称WWF)提供了一个编程框架和工具以开发和执行各种不同的基于工作流的应用程序,比如文档管理、线型的商业应用、贸易单据流程、IT管理、B2B应用以及消费者应用。
有状态的、持久化的、不间断运行的应用程序
WWF简化了创造有状态的,不间断运行的异步工作流应用程序的过程。WWF运行时引擎管理工作流的运行,为工作流的长期运行提供保障,并能抵抗机器的重启。WWF运行时服务提供了一系列的附加功能,例如WWF服务为能温和且正确的处理错误提供了事务和持久化。
工作流模型
WWF为开发人员提供了一个工作流模型,来描述应用程序所需要的处理过程。通过使用工作流模型所提供的流程控件、状态管理、事务和同步器,开发人员可以分离应用程序逻辑和业务逻辑,构造一个高层次的抽象,达到提高开发者效率的目的。
组件的重用
WWF为开发者提供了一系列的活动——活动是一种包含了工作单元的可配置逻辑结构。这种结构封装了开发者可能经常性用到的一些部件,这样就节省了开发者的时间。
如果遇到一些特殊的需求或场景,WWF同样为开发自定义的活动提供了简单的方法。
通过将工作流引擎载入进程,WWF可以使任何应用程序和服务容器运行工作流。
运行时服务组件被设计成可插件形式的,这个可使应用程序以最合适的方式来提供它们的服务。WWF还提供了一组运行时服务的默认实现,这些服务能满足大部分类型的应用程序。
另外,WWF还提供了对ASP.NET的out-of-the-box(啥意思?)支持,让构造和运行能在IIS和ASP.NET环境的工作流变得简单。
Windows Workflow Foundation之旅(二)——指南1(构造一个顺序工作流)
翻译自 ms-help://MS.WinWF.v1.EN/WinWF_GettingStarted/html/9c3e5551-4eff-4977-89ac-f81ab092d996.htm
顺序工作流(sequential workflow)是为执行一种由一系列预定义的步骤组成的任务而设计的。这种体系结构是模拟基于过程的应用程序的。这一节将用几个步骤来编写一个简单的开支报告程序,这个小程序使用WinFrom做界面,用顺序工作流做业务逻辑。
这个小程序有一个TextBox来输入开支报告的总数,一个Button点击提交报告。工作流将评估开支,如果开支小于1000则提请领班审批,如果大于等于1000则提请经理审批。之后,工作流会发送一个审批意见,此时,出现一个Label显示审批意见,两个Button分别表示通过和拒绝审批。当某一个按钮被点击的时候,应用程序会通知回应工作流,工作流继续处理发生的事件。
开始构造顺序工作流
创建工作流类
WWF SDK中定义了一个SequentialWorkFlow类,我们定义一个ExpenseRoportWorkflow类,并继承自SequentialWorkflow,这样就创建一个顺序工作流。如:
声明工作流参数
1
using System;
2
using System.Workflow.Activities;
3
using System.Workflow.Activities.Rules;
4![](images/None.gif)
5
namespace Microsoft.Samples.Workflow.Quickstarts.SequentialWorkflow
6![](images/ExpandedBlockStart.gif)
![](images/ContractedBlock.gif)
{
7
[RuleConditionsAttribute(typeof(Microsoft.Samples.
Workflow.Quickstarts.SequentialWorkflow.ExpenseReportWorkflow))]
8
public sealed partial class ExpenseReportWorkflow :
System.Workflow.Activities.SequentialWorkflow
9![](images/ExpandedSubBlockStart.gif)
{
10
public ExpenseReportWorkflow()
11![](images/ExpandedSubBlockStart.gif)
{
12![](images/InBlock.gif)
13
}
14
}
15
}
16![](images/None.gif)
在一个工作流运行时,它可以从宿主应用程序中接收参数。参数是ParameterDeclaration类型的对象,一旦工作流初始化完成,参数的值就能通过工作流的Parameters集合来访问。
这里的开始报告程序用了两个参数。第一个参数是开支的总数;第二个是一个传出参数,用来放置审批意见。
定义一个新的方法InitializeComponent,并在构造ExpenseRoportWorkflow类的构造函数中调用它。一下的例子示范了怎样定义两个参数并把它们加到Parameters集合中。
![](images/ContractedBlock.gif)
![](images/ExpandedBlockStart.gif)
1
public ExpenseReportWorkflow()
2![](images/None.gif)
3![](images/ExpandedBlockStart.gif)
![](images/ContractedBlock.gif)
{
4![](images/InBlock.gif)
5
InitializeComponent();
6![](images/InBlock.gif)
7
}
8![](images/None.gif)
9
10![](images/None.gif)
11
private void InitializeComponent()
12![](images/None.gif)
13![](images/ExpandedBlockStart.gif)
![](images/ContractedBlock.gif)
{
14![](images/InBlock.gif)
15
System.Workflow.ComponentModel.ParameterDeclaration Amount =
new System.Workflow.ComponentModel.ParameterDeclaration();
16![](images/InBlock.gif)
17
System.Workflow.ComponentModel.ParameterDeclaration Result =
new System.Workflow.ComponentModel.ParameterDeclaration();
18![](images/InBlock.gif)
19
//
20![](images/InBlock.gif)
21
// Workflow Parameters
22![](images/InBlock.gif)
23
//
24![](images/InBlock.gif)
25
Amount.Direction = System.Workflow.ComponentModel.ParameterDirection.In;
26![](images/InBlock.gif)
27
Amount.Name = "Amount";
28![](images/InBlock.gif)
29
Amount.Type = typeof(int);
30![](images/InBlock.gif)
31
Amount.Value = null;
32![](images/InBlock.gif)
33
Result.Direction = System.Workflow.ComponentModel.ParameterDirection.Out;
34![](images/InBlock.gif)
35
Result.Name = "Result";
36![](images/InBlock.gif)
37
Result.Type = typeof(string);
38![](images/InBlock.gif)
39
Result.Value = null;
40![](images/InBlock.gif)
41
this.Parameters.Add(Amount);
42![](images/InBlock.gif)
43
this.Parameters.Add(Result);
44![](images/InBlock.gif)
45
}
46![](images/None.gif)
47![](images/None.gif)
使用IfElse活动
IfElse活动用条件表达式来控制工作流中流程的运行。工作流将根据条件表达式的结果来决定执行条件分支(IfElseBranch)中的哪一个活动。
例子中将使用IfElse活动。通过判断从宿主应用程序中传入的Amount参数的值是否小于1000,来决定是否将审报发送到领班,否则发送到经理。
创建IfElse活动
1.定义4个私有变量
类型
|
名称 |
IfElse |
evaluateExpenseReportAmount |
IfElseBranch |
ifNeedsLeadApproval |
IfElseBranch |
elseNeedsManagerApproval |
CodeCondition |
ifElseLogicStatement |
2.在InitializeComponent中用默认构造函数实例以上4个对象。
以下的代码示例了怎样创建IfElse活动,并用IfElseBranch活动联系两个逻辑分支。你需要把以下代码放到InitializeComponent方法底部。
1
//
2![](images/None.gif)
3
// EvaluateExpenseReportAmount
4![](images/None.gif)
5
//
6![](images/None.gif)
7
this.EvaluateExpenseReportAmount.Activities.Add(this.ifNeedsLeadApproval);
8![](images/None.gif)
9
this.EvaluateExpenseReportAmount.Activities.Add(this.elseNeedsManagerApproval);
10![](images/None.gif)
11
this.EvaluateExpenseReportAmount.ID = "EvaluateExpenseReportAmount";
12![](images/None.gif)
13
//
14![](images/None.gif)
15
// ifNeedsLeadApproval
16![](images/None.gif)
17
//
18![](images/None.gif)
19
this.ifNeedsLeadApproval.Activities.Add(this.invokeGetLeadApproval);
20![](images/None.gif)
21
ifElseLogicStatement.Condition += new System.Workflow.Activities.
ConditionalExpression(this.DetermineApprovalContact);
22![](images/None.gif)
23
this.ifNeedsLeadApproval.Condition = ifElseLogicStatement;
24![](images/None.gif)
25
this.ifNeedsLeadApproval.ID = "ifNeedsLeadApproval";
26![](images/None.gif)
27
//
28![](images/None.gif)
29
// elseNeedsManagerApproval
30![](images/None.gif)
31
//
32![](images/None.gif)
33
this.elseNeedsManagerApproval.Activities.Add(this.invokeGetManagerApproval);
34![](images/None.gif)
35
this.elseNeedsManagerApproval.Condition = null;
36![](images/None.gif)
37
this.elseNeedsManagerApproval.ID = "elseNeedsManagerApproval";
38![](images/None.gif)
WWF在IfElse活动中,有两种评估条件表达式的方式。一种是RoleCondition,这个对象通过使用一组规则来判断条件表达式的结果;另一种就是使用CodeCondition活动。CodeCondition使用一个回调方法,这个回调方法返回一个代表评估结果的布尔值。上面的例子就是使用CodeCondition来决定条件表达式的值。如果Amount参数小于1000,回调方法返回true,否则返回false。以下的代码就是这个回调函数的定义,你可以把它加到工作流类的定义中。
1
private bool DetermineApprovalContact(object sender, EventArgs e)
2![](images/None.gif)
3![](images/ExpandedBlockStart.gif)
![](images/ContractedBlock.gif)
{
4![](images/InBlock.gif)
5
if ( Convert.ToInt32(this.Parameters["Amount"].Value) < 1000 )
6![](images/InBlock.gif)
7
return true;
8![](images/InBlock.gif)
9
10![](images/InBlock.gif)
11
return false;
12![](images/InBlock.gif)
13
}
14![](images/None.gif)
构造IfElse分支(IfElseBranch)活动
创建完IfElse活动之后,我们来构造IfElseBranch活动
在这个例子中,每一IfElse活动的分支都使用InvokeMethodActivity活动来通知宿主程序——工作流需要领班或经理的审批才能继续执行。InvokeMethodActivity被设计成调用一个在WWF运行时中的服务接口。我们在同一份代码文件中定义了这个接口。当我们在之后构造宿主程序时,宿主类将实现这个接口,以便能建立工作流和宿主程序的通信(这一段文档上写的很模糊,我reflect后看了源码才明白过来,在最后将补充描述一下)。
构建IfElseBranch活动
1. 在类中定义两个私有字段
类型
|
名称 |
InvokeMethodActivity |
invokeGetLeadApproval |
InvokeMethodActivity |
invokeGetManagerApproval |
2. 在InitializeComponent中用默认构造函数实例化这两个对象
以下的代码示例了怎样在父活动(IfElse)中创建IfElseBranch活动,并把两个的InvokeMethodActivity联系到对应的IfElseBranch活动上,每个InvokeMethodActivity将调用定义在IExpenseReportService接口中的方法,接口会在稍微实现。你需要把以下代码放到InitializeComponent方法底部。
1
//
2![](images/None.gif)
3
// invokeGetLeadApproval
4![](images/None.gif)
5
//
6![](images/None.gif)
7
this.invokeGetLeadApproval.ID = "invokeGetLeadApproval";
8![](images/None.gif)
9
this.invokeGetLeadApproval.InterfaceType = typeof(Microsoft.
Samples.Workflow.Quickstarts.SequentialWorkflow.IExpenseReportService);
10![](images/None.gif)
11
this.invokeGetLeadApproval.MethodName = "GetLeadApproval";
12![](images/None.gif)
13
//
14![](images/None.gif)
15
// invokeGetManagerApproval
16![](images/None.gif)
17
//
18![](images/None.gif)
19
this.invokeGetManagerApproval.ID = "invokeGetManagerApproval";
20![](images/None.gif)
21
this.invokeGetManagerApproval.InterfaceType = typeof(Microsoft.
Samples.Workflow.Quickstarts.SequentialWorkflow.IExpenseReportService);
22![](images/None.gif)
23
this.invokeGetManagerApproval.MethodName = "GetManagerApproval";
24![](images/None.gif)
25
以下代码定义了IExpenseReportService接口
1
using System;
2![](images/None.gif)
3
using System.Workflow.ComponentModel;
4![](images/None.gif)
5
using System.Workflow.Runtime.Messaging;
6![](images/None.gif)
7
8![](images/None.gif)
9
namespace Microsoft.Samples.Workflow.Quickstarts.SequentialWorkflow
10![](images/None.gif)
11![](images/ExpandedBlockStart.gif)
![](images/ContractedBlock.gif)
{
12![](images/InBlock.gif)
13
[DataExchangeService]
14![](images/InBlock.gif)
15
public interface IExpenseReportService
16![](images/InBlock.gif)
17![](images/ExpandedSubBlockStart.gif)
{
18![](images/InBlock.gif)
19
void GetLeadApproval();
20![](images/InBlock.gif)
21
void GetManagerApproval();
22![](images/InBlock.gif)
23
event EventHandler<WorkflowMessageEventArgs> ExpenseReportApproved;
24![](images/InBlock.gif)
25
event EventHandler<WorkflowMessageEventArgs> ExpenseReportRejected;
26![](images/InBlock.gif)
27
}
28![](images/InBlock.gif)
29
}
30![](images/None.gif)
31![](images/None.gif)
监听宿主事件
在这个阶段,工作流已经从宿主程序接受了两个参数(译者注:其中一个为out参数,此时设为null),评估了Amount参数,作出了到底该提请谁确认审批的决定,并通知了宿主程序在继续接下来的处理之前,确认审批。这里,Listen活动和EventSinkActivity活动往往配合使用,来监听宿主程序触发指定的事件。接着,一个approval或rejection事件被引发,工作流继续执行,返回审批结果Result,并终止流程。
Listen活动的每个分支是一个EventDriven活动。EventDriven活动只能使用实现了IEventActivity接口的活动。Listen活动的每个分支中的EventDriven各有一个EventSinkActivity,它们是用来监听宿主程序触发的ExpenseReportApproved或者ExpenseReportRejected事件的。这种工作流和宿主的通信方法其实类似于之前的InvokeMethodActivity的过程,只不过前者是工作流监听宿主事件,而后者是宿主事件工作流中注册的接口。
我们已经在前面的步骤中,把接口和两个事件的定义都完成了。在这里,我们将创建一个Listen活动并和两个EventDriven分支建立连接。每个分支包含一个EventSinkActivity活动,每个EventSink监听一种对应的事件。此外,我们还将创建一些事件处理程序,来处理AfterInvoke事件(译者注:这里的AfterInvoke事件应为Invoked事件)。这些事件处理程序将会把Result参数的值设为approval或者rejected。
构造监听活动
1.在工作流类中定义5个私有字段
类型
|
名称 |
Listen
|
listenApproveReject |
EventDriven |
approveEventDriven |
EventDriven |
rejectEventDriven |
EventSinkActivity |
approveEvent |
EventSinkActivity |
rejectEvent |
2. 在InitializeComponent中实例化。
以下的代码示例了怎样在创建Listen活动和EventSinkActivity活动,来监听宿主程序发出的事件。你需要把以下代码放到InitializeComponent方法底部。
1
//
2![](images/None.gif)
3
// listenApproveReject
4![](images/None.gif)
5
//
6![](images/None.gif)
7
this.listenApproveReject.Activities.Add(this.approveEventDriven);
8![](images/None.gif)
9
this.listenApproveReject.Activities.Add(this.rejectEventDriven);
10![](images/None.gif)
11
this.listenApproveReject.ID = "listenApproveReject";
12![](images/None.gif)
13
//
14![](images/None.gif)
15
// approveEventDriven
16![](images/None.gif)
17
//
18![](images/None.gif)
19
this.approveEventDriven.Activities.Add(this.approveEvent);
20![](images/None.gif)
21
this.approveEventDriven.ID = "approveEventDriven";
22![](images/None.gif)
23
//
24![](images/None.gif)
25
// approveEvent
26![](images/None.gif)
27
//
28![](images/None.gif)
29
this.approveEvent.EventName = "ExpenseReportApproved";
30![](images/None.gif)
31
this.approveEvent.ID = "approveEvent";
32![](images/None.gif)
33
this.approveEvent.InterfaceType = typeof(Microsoft.Samples.
Workflow.Quickstarts.SequentialWorkflow.IExpenseReportService);
34![](images/None.gif)
35
this.approveEvent.Roles = null;
36![](images/None.gif)
37
this.approveEvent.Invoked += new System.EventHandler
(this.approveEvent_Invoked);
38![](images/None.gif)
39
//
40![](images/None.gif)
41
// rejectEventDriven
42![](images/None.gif)
43
//
44![](images/None.gif)
45
this.rejectEventDriven.Activities.Add(this.rejectEvent);
46![](images/None.gif)
47
this.rejectEventDriven.ID = "rejectEventDriven";
48![](images/None.gif)
49
//
50![](images/None.gif)
51
// rejectEvent
52![](images/None.gif)
53
//
54![](images/None.gif)
55
this.rejectEvent.EventName = "ExpenseReportRejected";
56![](images/None.gif)
57
this.rejectEvent.ID = "rejectEvent";
58![](images/None.gif)
59
this.rejectEvent.InterfaceType = typeof(Microsoft.Samples.
Workflow.Quickstarts.SequentialWorkflow.IExpenseReportService);
60![](images/None.gif)
61
this.rejectEvent.Roles = null;
62![](images/None.gif)
63
this.rejectEvent.Invoked += new System.EventHandler
(this.rejectEvent_Invoked);
64![](images/None.gif)
65![](images/None.gif)
使用EventSinkActivity时,为了在工作流中加入一些附加逻辑,你可以为Invoked事件创建一个事件处理程序。一下是事件处理程序的代码。
1
private void approveEvent_Invoked(object sender, EventArgs e)
2![](images/None.gif)
3![](images/ExpandedBlockStart.gif)
![](images/ContractedBlock.gif)
{
4![](images/InBlock.gif)
5
this.Parameters["Result"].Value = "Report Approved";
6![](images/InBlock.gif)
7
}
8![](images/None.gif)
9
10![](images/None.gif)
11
private void rejectEvent_Invoked(object sender, EventArgs e)
12![](images/None.gif)
13![](images/ExpandedBlockStart.gif)
![](images/ContractedBlock.gif)
{
14![](images/InBlock.gif)
15
this.Parameters["Result"].Value = "Report Rejected";
16![](images/InBlock.gif)
17
}
18![](images/None.gif)
19![](images/None.gif)
完成顺序工作流
这个工作流包括两个主要的步骤:第一,监听宿主程序的递交审批事件,并把传入的值作为工作流参数;第二,监听通过或拒绝审批消息。一下的代码示例了怎样通过把之前创建好的活动加到工作流活动集中,来完成工作流的构造。
1
//
2![](images/None.gif)
3
// ExpenseReportWorkflow
4![](images/None.gif)
5
//
6![](images/None.gif)
7
this.Activities.Add(this.EvaluateExpenseReportAmount);
8![](images/None.gif)
9
this.Activities.Add(this.listenApproveReject);
10![](images/None.gif)
11
this.DynamicUpdateCondition = null;
12![](images/None.gif)
13
this.ID = "ExpenseReportWorkflow";
14![](images/None.gif)
15![](images/None.gif)
创建宿主程序
WWF需要一个宿主程序来运行工作流。当程序开始运行,WWF运行时引擎也随之启动。而之前构造好的工作流,则到用户点击了Submit按钮后才真正启动。
建立一个新的源文件,取名Program。以下的代码包含了完整的WinForm应用程序。IExpenseReportService接口GetLeadApproval和GetmanagerApproval方法已经定义在另一个文件中。宿主程序实现了这个接口。在approval和rejected按钮的click事件中,将引发ExpenseReprotApproval或ExpenseReprotRejected事件。
1
using System;
2![](images/None.gif)
3
using System.ComponentModel;
4![](images/None.gif)
5
using System.Drawing;
6![](images/None.gif)
7
using System.Windows.Forms;
8![](images/None.gif)
9
using System.Collections.Generic;
10![](images/None.gif)
11
using System.Workflow.Runtime;
12![](images/None.gif)
13
using System.Workflow.Runtime.Hosting;
14![](images/None.gif)
15
using System.Workflow.Runtime.Messaging;
16![](images/None.gif)
17
18![](images/None.gif)
19
namespace Microsoft.Samples.Workflow.Quickstarts.SequentialWorkflowHost
20![](images/None.gif)
21![](images/ExpandedBlockStart.gif)
![](images/ContractedBlock.gif)
{
22![](images/InBlock.gif)
23
public class MainForm : Form, Microsoft.Samples.
Workflow.Quickstarts.SequentialWorkflow.
IExpenseReportService
24![](images/InBlock.gif)
25![](images/ExpandedSubBlockStart.gif)
{
26![](images/InBlock.gif)
27
private System.Windows.Forms.Label label1;
28![](images/InBlock.gif)
29
private System.Windows.Forms.TextBox result;
30![](images/InBlock.gif)
31
private System.Windows.Forms.Label label2;
32![](images/InBlock.gif)
33
private System.Windows.Forms.Button submitButton;
34![](images/InBlock.gif)
35
private System.Windows.Forms.Label approvalState;
36![](images/InBlock.gif)
37
private System.Windows.Forms.Button approveButton;
38![](images/InBlock.gif)
39
private System.Windows.Forms.Button rejectButton;
40![](images/InBlock.gif)
41
private System.Windows.Forms.TextBox amount;
42![](images/InBlock.gif)
43
private System.Windows.Forms.Panel panel1;
44![](images/InBlock.gif)
45
46![](images/InBlock.gif)
47
private System.ComponentModel.IContainer components = null;
48![](images/InBlock.gif)
49
50![](images/InBlock.gif)
51
private delegate void GetApprovalDelegate();
52![](images/InBlock.gif)
53
private WorkflowRuntime workflowRuntime = null;
54![](images/InBlock.gif)
55
private WorkflowInstance workflowInstance = null;
56![](images/InBlock.gif)
57
58![](images/InBlock.gif)
59
public MainForm()
60![](images/InBlock.gif)
61![](images/ExpandedSubBlockStart.gif)
{
62![](images/InBlock.gif)
63
InitializeComponent();
64![](images/InBlock.gif)
65
66![](images/InBlock.gif)
67
// Collapse approve/reject panel
68![](images/InBlock.gif)
69
this.Height -= this.panel1.Height;
70![](images/InBlock.gif)
71
72![](images/InBlock.gif)
73
workflowRuntime = new WorkflowRuntime();
74![](images/InBlock.gif)
75
workflowRuntime.AddService(this);
76![](images/InBlock.gif)
77
workflowRuntime.StartRuntime();
78![](images/InBlock.gif)
79
80![](images/InBlock.gif)
81
workflowRuntime.WorkflowCompleted += new EventHandler
<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
82![](images/InBlock.gif)
83
}
84![](images/InBlock.gif)
85
86![](images/InBlock.gif)
87
protected override void Dispose(bool disposing)
88![](images/InBlock.gif)
89![](images/ExpandedSubBlockStart.gif)
{
90![](images/InBlock.gif)
91
if (disposing && (components != null))
92![](images/InBlock.gif)
93![](images/ExpandedSubBlockStart.gif)
{
94![](images/InBlock.gif)
95
components.Dispose();
96![](images/InBlock.gif)
97
}
98![](images/InBlock.gif)
99
base.Dispose(disposing);
100![](images/InBlock.gif)
101
}
102![](images/InBlock.gif)
103
104![](images/InBlock.gif)
105
private void InitializeComponent()
106![](images/InBlock.gif)
107![](images/ExpandedSubBlockStart.gif)
{
108![](images/InBlock.gif)
109
this.label1 = new System.Windows.Forms.Label();
110![](images/InBlock.gif)
111
this.result = new System.Windows.Forms.TextBox();
112![](images/InBlock.gif)
113
this.label2 = new System.Windows.Forms.Label();
114![](images/InBlock.gif)
115
this.submitButton = new System.Windows.Forms.Button();
116![](images/InBlock.gif)
117
this.approvalState = new System.Windows.Forms.Label();
118![](images/InBlock.gif)
119
this.approveButton = new System.Windows.Forms.Button();
120![](images/InBlock.gif)
121
this.rejectButton = new System.Windows.Forms.Button();
122![](images/InBlock.gif)
123
this.amount = new System.Windows.Forms.TextBox();
124![](images/InBlock.gif)
125
this.panel1 = new System.Windows.Forms.Panel();
126![](images/InBlock.gif)
127
this.panel1.SuspendLayout();
128![](images/InBlock.gif)
129
this.SuspendLayout();
130![](images/InBlock.gif)
131
//
132![](images/InBlock.gif)
133
// label1
134![](images/InBlock.gif)
135
//
136![](images/InBlock.gif)
137
this.label1.AutoSize = true;
138![](images/InBlock.gif)
139
this.label1.Location = new System.Drawing.Point(13, 13);
140![](images/InBlock.gif)
141
this.label1.Name = "label1";
142![](images/InBlock.gif)
143
this.label1.Size = new System.Drawing.Size(39, 13);
144![](images/InBlock.gif)
145
this.label1.TabIndex = 1;
146![](images/InBlock.gif)
147
this.label1.Text = "Amount";
148![](images/InBlock.gif)
149
//
150![](images/InBlock.gif)
151
// result
152![](images/InBlock.gif)
153
//
154![](images/InBlock.gif)
155
this.result.Location = new System.Drawing.Point(13, 69);
156![](images/InBlock.gif)
157
this.result.Name = "result";
158![](images/InBlock.gif)
159
this.result.ReadOnly = true;
160![](images/InBlock.gif)
161
this.result.Size = new System.Drawing.Size(162, 20);
162![](images/InBlock.gif)
163
this.result.TabIndex = 1;
164![](images/InBlock.gif)
165
this.result.TabStop = false;
166![](images/InBlock.gif)
167
//
168![](images/InBlock.gif)
169
// label2
170![](images/InBlock.gif)
171
//
172![](images/InBlock.gif)
173
this.label2.AutoSize = true;
174![](images/InBlock.gif)
175
this.label2.Location = new System.Drawing.Point(13, 54);
176![](images/InBlock.gif)
177
this.label2.Name = "label2";
178![](images/InBlock.gif)
179
this.label2.Size = new System.Drawing.Size(33, 13);
180![](images/InBlock.gif)
181
this.label2.TabIndex = 3;
182![](images/InBlock.gif)
183
this.label2.Text = "Result";
184![](images/InBlock.gif)
185
//
186![](images/InBlock.gif)
187
// submitButton
188![](images/InBlock.gif)
189
//
190![](images/InBlock.gif)
191
this.submitButton.Enabled = false;
192![](images/InBlock.gif)
193
this.submitButton.Location = new System.Drawing.Point(56, 95);
194![](images/InBlock.gif)
195
this.submitButton.Name = "submitButton";
196![](images/InBlock.gif)
197
this.submitButton.Size = new System.Drawing.Size(75, 23);
198![](images/InBlock.gif)
199
this.submitButton.TabIndex = 2;
200![](images/InBlock.gif)
201
this.submitButton.Text = "Submit";
202![](images/InBlock.gif)
203
this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
204![](images/InBlock.gif)
205
//
206![](images/InBlock.gif)
207
// approvalState
208![](images/InBlock.gif)
209
//
210![](images/InBlock.gif)
211
this.approvalState.AutoSize = true;
212![](images/InBlock.gif)
213
this.approvalState.Location = new System.Drawing.Point(10, 9);
214![](images/InBlock.gif)
215
this.approvalState.Name = "approvalState";
216![](images/InBlock.gif)
217
this.approvalState.Size = new System.Drawing.Size(45, 13);
218![](images/InBlock.gif)
219
this.approvalState.TabIndex = 4;
220![](images/InBlock.gif)
221
this.approvalState.Text = "Approval";
222![](images/InBlock.gif)
223
//
224![](images/InBlock.gif)
225
// approveButton
226![](images/InBlock.gif)
227
//
228![](images/InBlock.gif)
229
this.approveButton.Enabled = false;
230![](images/InBlock.gif)
231
this.approveButton.Location = new System.Drawing.Point(11, 25);
232![](images/InBlock.gif)
233
this.approveButton.Name = "approveButton";
234![](images/InBlock.gif)
235
this.approveButton.Size = new System.Drawing.Size(75, 23);
236![](images/InBlock.gif)
237
this.approveButton.TabIndex = 0;
238![](images/InBlock.gif)
239
this.approveButton.Text = "Approve";
240![](images/InBlock.gif)
241
this.approveButton.Click += new System.EventHandler(this.approveButton_Click);
242![](images/InBlock.gif)
243
//
244![](images/InBlock.gif)
245
// rejectButton
246![](images/InBlock.gif)
247
//
248![](images/InBlock.gif)
249
this.rejectButton.Enabled = false;
250![](images/InBlock.gif)
251
this.rejectButton.Location = new System.Drawing.Point(86, 25);
252![](images/InBlock.gif)
253
this.rejectButton.Name = "rejectButton";
254![](images/InBlock.gif)
255
this.rejectButton.Size = new System.Drawing.Size(75, 23);
256![](images/InBlock.gif)
257
this.rejectButton.TabIndex = 1;
258![](images/InBlock.gif)
259
this.rejectButton.Text = "Reject";
260![](images/InBlock.gif)
261
this.rejectButton.Click += new System.EventHandler(this.rejectButton_Click);
262![](images/InBlock.gif)
263
//
264![](images/InBlock.gif)
265
// amount
266![](images/InBlock.gif)
267
//
268![](images/InBlock.gif)
269
this.amount.Location = new System.Drawing.Point(13, 29);
270![](images/InBlock.gif)
271
this.amount.MaxLength = 9;
272![](images/InBlock.gif)
273
this.amount.Name = "amount";
274![](images/InBlock.gif)
275
this.amount.Size = new System.Drawing.Size(162, 20);
276![](images/InBlock.gif)
277
this.amount.TabIndex = 0;
278![](images/InBlock.gif)
279
this.amount.KeyPress += new System.Windows.Forms.
KeyPressEventHandler(this.amount_KeyPress);
280![](images/InBlock.gif)
281
this.amount.TextChanged += new System.EventHandler
(this.amount_TextChanged);
282![](images/InBlock.gif)
283
//
284![](images/InBlock.gif)
285
// panel1
286![](images/InBlock.gif)
287
//
288![](images/InBlock.gif)
289
this.panel1.Controls.Add(this.approvalState);
290![](images/InBlock.gif)
291
this.panel1.Controls.Add(this.approveButton);
292![](images/InBlock.gif)
293
this.panel1.Controls.Add(this.rejectButton);
294![](images/InBlock.gif)
295
this.panel1.Location = new System.Drawing.Point(3, 124);
296![](images/InBlock.gif)
297
this.panel1.Name = "panel1";
298![](images/InBlock.gif)
299
this.panel1.Size = new System.Drawing.Size(172, 66);
300![](images/InBlock.gif)
301
this.panel1.TabIndex = 8;
302![](images/InBlock.gif)
303
//
304![](images/InBlock.gif)
305
// MainForm
306![](images/InBlock.gif)
307
//
308![](images/InBlock.gif)
309
this.AcceptButton = this.submitButton;
310![](images/InBlock.gif)
311
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
312![](images/InBlock.gif)
313
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
314![](images/InBlock.gif)
315
this.ClientSize = new System.Drawing.Size(187, 201);
316![](images/InBlock.gif)
317
this.Controls.Add(this.panel1);
318![](images/InBlock.gif)
319
this.Controls.Add(this.amount);
320![](images/InBlock.gif)
321
this.Controls.Add(this.submitButton);
322![](images/InBlock.gif)
323
this.Controls.Add(this.label2);
324![](images/InBlock.gif)
325
this.Controls.Add(this.result);
326![](images/InBlock.gif)
327
this.Controls.Add(this.label1);
328![](images/InBlock.gif)
329
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
330![](images/InBlock.gif)
331
this.MaximizeBox = false;
332![](images/InBlock.gif)
333
this.MinimizeBox = false;
334![](images/InBlock.gif)
335
this.Name = "MainForm";
336![](images/InBlock.gif)
337
this.Text = "Simple Expense Report";
338![](images/InBlock.gif)
339
this.panel1.ResumeLayout(false);
340![](images/InBlock.gif)
341
this.panel1.PerformLayout();
342![](images/InBlock.gif)
343
this.ResumeLayout(false);
344![](images/InBlock.gif)
345
this.PerformLayout();
346![](images/InBlock.gif)
347
348![](images/InBlock.gif)
349
}
350![](images/InBlock.gif)
351
352![](images/InBlock.gif)
353
private void submitButton_Click(object sender, EventArgs e)
354![](images/InBlock.gif)
355![](images/ExpandedSubBlockStart.gif)
{
356![](images/InBlock.gif)
357
Type type = typeof(Microsoft.Samples.Workflow.Quickstarts.SequentialWorkflow.ExpenseReportWorkflow);
358![](images/InBlock.gif)
359
360![](images/InBlock.gif)
361
// Construct workflow parameters
362![](images/InBlock.gif)
363
Dictionary<string, object> properties = new Dictionary<string, object>();
364![](images/InBlock.gif)
365
properties.Add("Amount", Int32.Parse(this.amount.Text));
366![](images/InBlock.gif)
367
properties.Add("Result", string.Empty);
368![](images/InBlock.gif)
369
370![](images/InBlock.gif)
371
// Start the workflow
372![](images/InBlock.gif)
373
workflowInstance = workflowRuntime.StartWorkflow(type, properties);
374![](images/InBlock.gif)
375
}
376![](images/InBlock.gif)
377
378![](images/InBlock.gif)
379
void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
380![](images/InBlock.gif)
381![](images/ExpandedSubBlockStart.gif)
{
382![](images/InBlock.gif)
383
if (this.result.InvokeRequired)
384![](images/InBlock.gif)
385
this.result.Invoke(new EventHandler<WorkflowCompletedEventArgs>
(this.workflowRuntime_WorkflowCompleted), sender, e);
386![](images/InBlock.gif)
387
else
388![](images/InBlock.gif)
389![](images/ExpandedSubBlockStart.gif)
{
390![](images/InBlock.gif)
391
this.result.Text = e.OutputParameters["Result"].ToString();
392![](images/InBlock.gif)
393
394![](images/InBlock.gif)
395
// Clear fields
396![](images/InBlock.gif)
397
this.amount.Text = string.Empty;
398![](images/InBlock.gif)
399
400![](images/InBlock.gif)
401
// Disable buttons
402![](images/InBlock.gif)
403
this.approveButton.Enabled = false;
404![](images/InBlock.gif)
405
this.rejectButton.Enabled = false;
406![](images/InBlock.gif)
407
}
408![](images/InBlock.gif)
409
}
410![](images/InBlock.gif)
411
412![](images/InBlock.gif)
413
private void approveButton_Click(object sender, EventArgs e)
414![](images/InBlock.gif)
415![](images/ExpandedSubBlockStart.gif)
{
416![](images/InBlock.gif)
417
// Raise the ExpenseReportApproved event back to the workflow
418![](images/InBlock.gif)
419
ExpenseReportApproved(null, new WorkflowMessageEventArgs
(this.workflowInstance.InstanceId));
420![](images/InBlock.gif)
421
this.Height -= this.panel1.Height;
422![](images/InBlock.gif)
423
this.submitButton.Enabled = true;
424![](images/InBlock.gif)
425
}
426![](images/InBlock.gif)
427
428![](images/InBlock.gif)
429
private void rejectButton_Click(object sender, EventArgs e)
430![](images/InBlock.gif)
431![](images/ExpandedSubBlockStart.gif)
{
432![](images/InBlock.gif)
433
// Raise the ExpenseReportRejected event back to the workflow
434![](images/InBlock.gif)
435
ExpenseReportRejected(null, new WorkflowMessageEventArgs
(this.workflowInstance.InstanceId));
436![](images/InBlock.gif)
437
this.Height -= this.panel1.Height;
438![](images/InBlock.gif)
439
this.submitButton.Enabled = true;
440![](images/InBlock.gif)
441
}
442![](images/InBlock.gif)
443
444![](images/InBlock.gif)
445![](images/ContractedSubBlock.gif)
IExpenseReportService Members#region IExpenseReportService Members
446![](images/InBlock.gif)
447
448![](images/InBlock.gif)
449
public void GetLeadApproval()
450![](images/InBlock.gif)
451![](images/ExpandedSubBlockStart.gif)
{
452![](images/InBlock.gif)
453
if (this.approvalState.InvokeRequired)
454![](images/InBlock.gif)
455
this.approvalState.Invoke(new GetApprovalDelegate(this.GetLeadApproval));
456![](images/InBlock.gif)
457
else
458![](images/InBlock.gif)
459![](images/ExpandedSubBlockStart.gif)
{
460![](images/InBlock.gif)
461
this.approvalState.Text = "Lead approval needed";
462![](images/InBlock.gif)
463
this.approveButton.Enabled = true;
464![](images/InBlock.gif)
465
this.rejectButton.Enabled = true;
466![](images/InBlock.gif)
467
468![](images/InBlock.gif)
469
// expand the panel
470![](images/InBlock.gif)
471
this.Height += this.panel1.Height;
472![](images/InBlock.gif)
473
this.submitButton.Enabled = false;
474![](images/InBlock.gif)
475
}
476![](images/InBlock.gif)
477
}
478![](images/InBlock.gif)
479
480![](images/InBlock.gif)
481
public void GetManagerApproval()
482![](images/InBlock.gif)
483![](images/ExpandedSubBlockStart.gif)
{
484![](images/InBlock.gif)
485
if (this.approvalState.InvokeRequired)
486![](images/InBlock.gif)
487
this.approvalState.Invoke(new GetApprovalDelegate(this.GetManagerApproval));
488![](images/InBlock.gif)
489
else
490![](images/InBlock.gif)
491![](images/ExpandedSubBlockStart.gif)
{
492![](images/InBlock.gif)
493
this.approvalState.Text = "Manager approval needed";
494![](images/InBlock.gif)
495
this.approveButton.Enabled = true;
496![](images/InBlock.gif)
497
this.rejectButton.Enabled = true;
498![](images/InBlock.gif)
499
500![](images/InBlock.gif)
501
// expand the panel
502![](images/InBlock.gif)
503
this.Height += this.panel1.Height;
504![](images/InBlock.gif)
505
this.submitButton.Enabled = false;
506![](images/InBlock.gif)
507
}
508![](images/InBlock.gif)
509
}
510![](images/InBlock.gif)
511
512![](images/InBlock.gif)
513
public event EventHandler<WorkflowMessageEventArgs> ExpenseReportApproved;
514![](images/InBlock.gif)
515
public event EventHandler<WorkflowMessageEventArgs> ExpenseReportRejected;
516![](images/InBlock.gif)
517
518![](images/InBlock.gif)
519
#endregion
520![](images/InBlock.gif)
521
522![](images/InBlock.gif)
523
private void amount_KeyPress(object sender, KeyPressEventArgs e)
524![](images/InBlock.gif)
525![](images/ExpandedSubBlockStart.gif)
{
526![](images/InBlock.gif)
527
if (!Char.IsControl(e.KeyChar) && (!Char.IsDigit(e.KeyChar)))
528![](images/InBlock.gif)
529
e.KeyChar = Char.MinValue;
530![](images/InBlock.gif)
531
}
532![](images/InBlock.gif)
533
534![](images/InBlock.gif)
535
private void amount_TextChanged(object sender, EventArgs e)
536![](images/InBlock.gif)
537![](images/ExpandedSubBlockStart.gif)
{
538![](images/InBlock.gif)
539
submitButton.Enabled = amount.Text.Length > 0;
540![](images/InBlock.gif)
541
}
542![](images/InBlock.gif)
543
}
544![](images/InBlock.gif)
545
546![](images/InBlock.gif)
547
static class Program
548![](images/InBlock.gif)
549![](images/ExpandedSubBlockStart.gif)
{
550![](images/InBlock.gif)
551![](images/ExpandedSubBlockStart.gif)
/**//// <summary>
552![](images/InBlock.gif)
553
/// The main entry point for the application.
554![](images/InBlock.gif)
555
/// </summary>
556![](images/InBlock.gif)
557
[STAThread]
558![](images/InBlock.gif)
559
static void Main()
560![](images/InBlock.gif)
561![](images/ExpandedSubBlockStart.gif)
{
562![](images/InBlock.gif)
563
Application.EnableVisualStyles();
564![](images/InBlock.gif)
565
Application.Run(new MainForm());
566![](images/InBlock.gif)
567
}
568![](images/InBlock.gif)
569
}
570![](images/InBlock.gif)
571
}
572![](images/None.gif)
573![](images/None.gif)
Ps:上面还有个问题没有解释清楚,我reflect了一下,看了源码才知道个大概。
那就是IfElseBranch中的InvokeMethodActivity。
InvokeMethodActivity中有一个Type类型的InterfaceType属性,使用时,需要设置这个属性,并把MethodName设为这个接口中的一个方法的名称。运行时,工作流引擎(也就是WorkflowRuntime)将通过反射调用这个接口。
但引擎怎么知道调用接口的哪个实现呢?你看
workflowRuntime = new WorkflowRuntime();
workflowRuntime.AddService(this);
workflowRuntime.StartRuntime();
原来,初始化引擎时,我们已经把实现了这个interface的类型的实例(this)注册到工作流中了。运行时,引擎就遍历所有已经注册的服务,如果实现了这个接口,这调用它。
另外,可以注册一个以上的服务,工作流引擎将调用所有实现了相应接口的接口方法。