Windows Workflow 4:旧瓶装新酒
转自:作者 Jonathan Allen 译者 张龙
Windows Workflow 4基本上对原有的程序库进行了重写。虽然目标一样,都是为长时间运行的任务提供一种模型语言,但重写之后的程序库还是有很多与众不同的地方。首先要说的是向后兼容性:完全没有。我们可以在WF4中通过Interopt活动来调用WF3工作流,但却有很多限制。比如说,我们无法使用WF3的Send和Receive活动,也无法暂停工作流或是调用其他工作流。
WF4对WF3的重写是项浩大的工程,前者完全使用XAML来定义工作流,这么做的结果就是代码的生成更加容易,但也意味着我们无法再使用Code活动了。WF4并不支持State Machine工作流,因此基于State Machine的所有内容都要重新设计了。WF4处理错误的手段也发生了很大的变化,由于这个原因,Suspend和Terminate活动也一去不复返了。还有很多其他的活动也发生了名称或是语义上的变化。Matt Milner从去年7月起所维护的变化列表是最能反映这种变化的。
Activities with direct or indirect equivalents
WF3 Activity | WF4 Activity | Notes |
Delay | Delay | The activity works the same, but the timers are handled differently in the framework. |
Sequence | Sequence |
|
Parallel | Parallel | Similar, but the internals of execution may differ slightly by RTM. |
Replicator | ForEach<T>, ParallelForEach<T>, | These provide <optional> typed access to the instance data and truly declarative authoring experience. Each represents the different execution modes of the Replicator activity – so it has been split into four similar activities. I think most people will use the generic versions more than the others, but time will tell. |
CallExternalMethod | InvokeMethod, InvokeMethod<T> | Provides .NET method invocation and optional return of a typed return value. This can be a call on an instance stored in a variable or a static method. |
HandleExternalEvent | Receive, ReceiveAndSendReply | WCF messaging activities replace the Local communications model, even for host-> workflow communication. |
Listen | Pick | The Pick activity is the primary WF4 activity that replaces the Listen and the State which were both containers for EventDriven activities. |
EventDriven | PickBranch | This is an indirect mapping and you don't use the PickBranch outside the pick, but it serves the same basic purpose of the EventDriven. |
Compensate | Compensate | Though the mechanism are slightly different, the activity serves the same purpose – to execute the compensation handler for a compensable scope. |
CompensatableSequence | CompensableActivity | The new activity includes a ConfirmationHandler which can execute when a confirmation is signaled using the Confirm activity. |
FaultHandler(s) | TryCatch | The try catch logic is more explicit now and you use the TryCatch activity to model your fault handling instead of using fault handlers on the composite activities. |
IfElse | If | In WF 4, this can only have two branches, the If and the Else. For more branches, use the switch activity. |
InvokeWebService | Send, SendAndReceiveReply | All web service communication in WF4 uses WCF. |
Throw | Throw |
|
TransactionScope | TransactionScopeActivity |
|
WebServiceInput (output and fault) | Receive, ReceiveAndSendReply | WCF is THE messaging system to use with WF. |
While | While/DoWhile | WF4 introduces the DoWhile in addition to the While to ensure the first iteration executes. |
WF3 Activities with no direct WF4 equivalent
WF3 Activity | Note |
ConditionedActivityGroup | Based on limited use (my guess) this activity was not moved to WF4. |
Code | There is no code-behind file for workflows so there is no place to write code in the workflow. Create custom activities or use expressions where appropriate. |
EventHandlingScope | No real equivalent, probably b/c this is an activity that gets overlooked or people use the state machine instead. |
InvokeWorkflow | In the Beta, there is no activity like this one. One option is to host child workflows as WCF services and use the Send or SendAndReceiveReply messaging activities to start the child workflows. |
Policy | In order to use rules in WF4, create a WF3 activity with a Policy activity inside it. Create properties on the activity and use them in the policy definition. Then use the InteropActivity to invoke the WF3 activity and execute the policy. You can use the properties on the activity as input and outputs to the policy. |
Suspend | In WF4 there is more focus on having a "suspend on error" style exception handling, so direct suspend is not currently supported in the form of an activity. |
SynchronizationScope | Again, my assumption here, this was not used a lot by folks so didn't get moved over. |
Terminate | No direct option to terminate, but exception handling has changed so that when a workflow throws an exception, you can abort, terminate or cancel it. |
CompensatableTransactionScope | In WF4, use a Compensable activity and put a TransactionScopeActivity in the body. |
State, StateInitialization, StateFinalization | There is no State Machine workflow in WF 4. |
WF4 activities with no direct WF3 equivalent
AddToCollection<T> | Helper activity to simplify declarative workflow development and manipulation of collection variables. |
Assign | Assigns a value to a variable – useful for declarative workflows. |
CancellationScope | Allows you to define a scope of work and the steps to take if that work is canceled. Replaces the cancelation handler in WF3. |
ClearCollection<T> |
|
Confirm | Schedules the Confirmation logic for a Compensable activity. |
ExistsInCollection<T> |
|
Persist | Explicit declaration of persistence from the workflow. Replaces the need for the PersistOnClose attribute on activities. |
RemoveFromCollection<T> |
|
Switch<T> | Provides multiple branches of execution each based on a specific result from evaluating an expression. |
Interop | Executes a WF3 activity in the context of a WF4 workflow. All public properties on the activity become In/Out arguments. Custom designers are not supported. |
如果你还是一个Windows Workflow菜鸟,没有使用过老版本,那么你是幸运的。WF4极大地简化了工作流托管、数据管理以及客户化活动的创建。使用依赖属性(dependency properties)在活动间传递数据的时代已经过去了。
Windows Workflow 4的一个主要卖点是与Windows Communication Foundation和Windows AppFabric的集成。微软设想的蓝图是:开发者通过外部的WCF和内部的WF构建消息应用,同时Windows Server AppFabric为其提供托管服务。
技术的进步确实令人欢喜令人优啊!