.NET 4.5 中的 Windows Workflow Foundation 中的新增功能What's New in Windows Workflow Foundation in .NET 4.5

.NET Framework 4.5 中的 Windows Workflow Foundation (WF)引入了许多新功能,例如新的活动、设计器功能和工作流开发模型。Windows Workflow Foundation (WF) in .NET Framework 4.5 introduces many new features, such as new activities, designer capabilities, and workflow development models. 在重新托管的工作流设计器中支持 .NET Framework 4.5 中引入的许多(但不是全部)新工作流功能。Many, but not all, of the new workflow features introduced in .NET Framework 4.5 are supported in the re-hosted workflow designer. 有关支持的新功能的详细信息,请参阅重新承载工作流设计器中对新 Workflow Foundation 4.5 功能的支持For more information about the new features that are supported, see Support for New Workflow Foundation 4.5 Features in the Rehosted Workflow Designer. 有关迁移 .NET 3.0 和 .NET 3.5 工作流应用程序以使用最新版本的详细信息,请参阅迁移指南For more information about migrating .NET 3.0 and .NET 3.5 workflow applications to use the latest version, see Migration Guidance. 本主题概述了 .NET Framework 4.5 中引入的新工作流功能。This topic provides an overview of the new workflow features introduced in .NET Framework 4.5.

警告

.NET Framework 4.5 中引入的新 Windows Workflow Foundation 功能对于面向以前版本 Framework 的项目不可用。The new Windows Workflow Foundation features introduced in .NET Framework 4.5 are not available for projects that target previous versions of the framework. 如果面向 .NET Framework 4.5 的项目将被重新定向到早期版本的框架,则可能会出现几个问题。If a project that targets .NET Framework 4.5 is re-targeted to a previous version of the framework, several issues can occur.

  • 在设计器中,c # 表达式将替换为XAML中的消息值。C# expressions will be replaced in the designer with the message Value was set in XAML.
  • 将发生许多生成错误,包括以下错误。Many build errors will occur, including the following error.

文件格式与当前目标框架不兼容。若要转换文件格式,请显式保存文件。保存文件并重新打开设计器后,此错误消息将消失。The file format is not compatible with current targeting framework. To convert the file format, please explicitly save the file. This error message will go away after you save the file and reopen the designer.

工作流版本控制Workflow Versioning

.NET Framework 4.5 基于新类引入了几个新的版本控制功能 WorkflowIdentity.NET Framework 4.5 introduced several new versioning features based around the new WorkflowIdentity class. WorkflowIdentity 为工作流应用程序提供了一种通过定义来映射持久化工作流实例的机制。WorkflowIdentity provides workflow application authors a mechanism for mapping a persisted workflow instance with its definition.

ActivityActivities

内置活动库包含新活动和现有活动的新功能。The built-in activity library contains new activities and new features for existing activities.

NoPersist 范围NoPersist Scope

NoPersistScope 是一个在执行 NoPersistScope 的子活动时防止工作流持久保存的新容器活动。NoPersistScope is a new container activity that prevents a workflow from being persisted when the NoPersistScope’s child activities are executing. 此活动在工作流不适合持久保存的方案中十分有用,例如,当工作流使用计算机特定资源(如文件句柄)时或在执行数据库事务期间。This is useful in scenarios where it is not appropriate for the workflow to be persisted, such as when the workflow is using machine-specific resources such as file handles, or during database transactions. 以前,为防止在活动执行期间出现持久性,需要一个使用 NativeActivity 的自定义 NoPersistHandlePreviously, to prevent persistence from occurring during an activity's execution, a custom NativeActivity that used a NoPersistHandle was required.

新的流程图功能New Flowchart Capabilities

流程图针对 .NET Framework 4.5 进行了更新,并具有以下新功能:Flowcharts are updated for .NET Framework 4.5 and have the following new capabilities:

  • DisplayNameFlowSwitch<T> 活动的 FlowDecision 属性是可编辑的。The DisplayName property of a FlowSwitch<T> or FlowDecision activity is editable. 这样,活动设计器就可以显示有关该活动的用途的详细信息。This will let the activity designer show more information about the activity's purpose.

  • 流程图具有一个名为 ValidateUnconnectedNodes 的新属性;此属性的默认值为 FalseFlowcharts have a new property called ValidateUnconnectedNodes; the default for this property is False. 如果此属性设置为 True,则未连接的流程图节点将产生验证错误。If this property is set to True, then unconnected flowchart nodes will produce validation errors.

支持部分信任Support for Partial Trust

.NET Framework 4 的工作流需要完全受信任的应用程序域。Workflows in .NET Framework 4 required a fully trusted application domain. 在 .NET Framework 4.5 中,工作流可在部分信任环境中运行。In .NET Framework 4.5, workflows can operate in a partial trust environment. 在部分信任环境中,可以使用第三方组件而无需为其授予主机资源的完全访问权限。In a partial trust environment, third-party components can be used without granting them full access to the resources of the host. 关于在部分信任中运行的工作流,请注意以下几点:Some concerns about running workflows in partial trust are as follows:

  1. 在部分信任下,不支持使用 Interop 活动中包含的旧组件(包括规则)。Using legacy components (including Rules) contained in the Interop activity is not supported under partial trust.

  2. 不支持 WorkflowServiceHost 中在部分信任下运行的工作流。Running workflows in partial trust in WorkflowServiceHost is not supported.

  3. 在部分信任方案中持久保留异常是一种潜在安全威胁。Persisting exceptions in a partial-trust scenario is a potential security threat. 若要禁用异常的持久保留,必须将扩展类型 ExceptionPersistenceExtension 添加至项目,以便取消持久化异常。To disable persisting of exceptions, an extension of type ExceptionPersistenceExtension must be added to the project in order to opt out of persisting exceptions. 下面的代码示例演示如何实现此类型。The following code example demonstrates how to implement this type.

    public class ExceptionPersistenceExtension
    {
        public ExceptionPersistenceExtension()
        {
            this.PersistExceptions = false;
        }
        public bool PersistExceptions { get; set; }
    }
    

    如果无需对异常进行序列化,请确保在 NoPersistScope 内使用异常。If exceptions are not to be serialized, ensure that exceptions are used within a NoPersistScope.

  4. 活动作者应重写 CacheMetadata 以避免让工作流运行时针对该类型自动执行反射。Activity authors should override CacheMetadata to avoid having the workflow runtime automatically execute reflection against the type. 参数和子活动必须为非 null,并且 Bind 必须被显式调用。Arguments and child activities must be non-null, and Bind must be called explicitly. 有关重写的详细信息 CacheMetadata ,请参阅用 CacheMetadata 公开数据For more information on overriding CacheMetadata, see Exposing data with CacheMetadata. 此外, internal 必须在中显式创建作为或私有类型的参数的实例, CacheMetadata 以避免通过反射进行创建。Also, instances of arguments that are of a type that is internal or private must be explicitly created in CacheMetadata to avoid being created by reflection.

  5. 这些类型不会使用 ISerializableSerializableAttribute 来进行序列化;要进行序列化的类型必须支持 DataContractSerializerTypes will not use ISerializable or SerializableAttribute for serialization; types that are to be serialized must support DataContractSerializer.

  6. 使用 LambdaValue<TResult> 的表达式需要 RestrictedMemberAccess,因此不能在部分信任下使用。Expressions that use LambdaValue<TResult> require RestrictedMemberAccess, and thus will not work under partial trust. 使用 LambdaValue<TResult> 的工作流应将这些表达式替换为派生自 CodeActivity<TResult> 的活动。Workflows that use LambdaValue<TResult> should replace those expressions with activities that derive from CodeActivity<TResult>. ..

  7. 在部分信任下,不能使用 TextExpressionCompiler 或 Visual Basic 承载的编译器来编译表达式,但是可以运行以前编译的表达式。Expressions cannot be compiled using TextExpressionCompiler or the Visual Basic hosted compiler in partial trust, but previously compiled expressions can be run.

  8. 使用2 级透明度的单个程序集不能在 .NET Framework 4、 .NET Framework 4.6.1.NET Framework 4.6.1 完全信任和部分信任模式下使用 .NET Framework 4.6.1.NET Framework 4.6.1A single assembly that uses Level 2 Transparency cannot be used in .NET Framework 4, .NET Framework 4.6.1.NET Framework 4.6.1 in full trust, and .NET Framework 4.6.1.NET Framework 4.6.1 in partial trust.

新设计器功能New Designer Capabilities

为了使较大的工作流程更易于管理,现在可以按关键字来搜索工作流。To make larger workflows more manageable, workflows can now be searched by keyword. 此功能仅在 Visual Studio 中提供;此功能在重新承载设计器中不可用。This feature is only available in Visual Studio; this feature is not available in a rehosted designer. 提供了两种搜索:There are two kinds of searches available:

  • "快速查找",通过Ctrl + F或 "编辑"、"查找和替换"、"快速查找" 启动。Quick Find, initiated with either Ctrl+F or Edit, Find and Replace, Quick Find.

  • "在文件中查找",通过按Ctrl + Shift + F或 "编辑"、"查找和替换"、"在文件中查找" 启动。Find in Files, initiated with either Ctrl+Shift+F or Edit, Find and Replace, Find in Files.

请注意,不支持替换。Note that Replace is not supported.

快速查找Quick Find

在工作流中搜索的关键字将匹配下列设计器项:Keywords searched in workflows will match the following designer items:

在设计器上的 ModelItem 树上执行快速查找。Quick Find is performed on the designer's ModelItem tree. 快速查找不会查找在工作流定义中导入的命名空间。Quick Find will not locate namespaces imported in the workflow definition.

在文件中查找Find in Files

在工作流中搜索的关键字将匹配工作流文件的实际内容。Keywords searched in workflows will match the actual content of the workflow files. 搜索结果将显示在 Visual Studio 查找结果视图窗格中。The search results will be shown in Visual Studio Find Results view pane. 双击结果项将会定位到包含工作流设计器中的匹配项的活动。Double-clicking the result item will navigate to the activity which contains the match in workflow designer.

删除变量和参数设计器中的上下文菜单项Delete context menu item in variable and argument designer

在 .NET Framework 4 中,只能使用键盘在设计器中删除变量和参数。In .NET Framework 4, variables and arguments could only be deleted in the designer using the keyboard. 从 .NET Framework 4.5 开始,可以使用上下文菜单来删除变量和参数。Starting with .NET Framework 4.5, variables and arguments can be deleted using the context menu.

以下屏幕快照显示了变量和自变量设计器的上下文菜单。The following screenshot shows the variable and argument designer context menu.

变量和参数设计器上下文菜单

序列自动环绕Auto-surround with Sequence

由于工作流或特定容器活动(如 NoPersistScope)只能包含单个主体活动,因此添加第二个活动需要开发人员删除第一个活动,请添加一个 Sequence 活动,然后将两个活动都添加到该顺序活动中。Since a workflow or certain container activities (such as NoPersistScope) can only contain a single body activity, adding a second activity required the developer to delete the first activity, add a Sequence activity, and then add both activities to the sequence activity. 从 .NET Framework 4.5 开始,在将第二个活动添加到设计器图面时, Sequence 将自动创建一个活动来包装这两个活动。Starting with .NET Framework 4.5, when adding a second activity to the designer surface, a Sequence activity will be automatically created to wrap both activities.

以下屏幕快照显示了 WriteLineBody 中的 NoPersistScope 活动。The following screenshot shows a WriteLine activity in the Body of a NoPersistScope.

NoPersistScope 活动正文中的 WriteLine 活动。

以下屏幕快照显示了在第一个 Sequence 下面丢弃第二个时在 Body 中自动创建的 WriteLine 活动。The following screenshot shows the automatically created Sequence activity in the Body when a second WriteLine is dropped below the first.

在 NoPersistScope 的正文中自动创建的序列。

平移模式Pan Mode

若要更轻松地在设计器中浏览大型工作流,可以启用平移模式;通过此模式,开发人员可以单击并拖动工作流的可见部分将其移动,而无需使用滚动条。To more easily navigate a large workflow in the designer, pan mode can be enabled, allowing the developer to click and drag to move the visible portion of the workflow, rather than needing to use the scroll bars. 用于激活平移模式的按钮位于设计器的右下角。The button to activate pan mode is in the lower right corner of the designer.

下面的屏幕快照显示了位于工作流设计器右下角的平移按钮。The following screenshot shows the pan button which is located at the bottom right corner of the workflow designer.

工作流设计器中突出显示的 "平移" 按钮。

也可以使用鼠标中键或空格键来平移工作流设计器。The middle mouse button or space bar can also be used to pan the workflow designer.

多重选择Multi-select

通过拖动围绕活动的矩形(未启用平移模式时)或通过按住 Ctrl 键并逐一单击所需的活动,可以一次选择多个活动。Multiple activities can be selected at one time, either by dragging a rectangle around them (when pan mode is not enabled), or by holding down Ctrl and click the desired activities one by one.

也可以在设计器中拖动并放置多个选择的活动,并使用上下文菜单与这些活动交互。Multiple activity selections can also be dragged and dropped within the designer, and can also be interacted with using the context menu.

工作流项的大纲视图Outline view of workflow items

为了更加方便地浏览分层工作流,工作流的组件显示在树样式的大纲视图中。In order to make hierarchical workflows easier to navigate, components of a workflow are shown in a tree-style outline view. 大纲视图显示在 "文档大纲" 视图中。The outline view is displayed in the Document Outline view. 若要打开此视图,请从顶部菜单中选择 "视图"、"其他窗口"、"文档大纲",或按 Ctrl W、U。To open this view, from the top menu, select View, Other Windows, Document Outline, or press Ctrl W,U. 单击大纲视图中的节点将会定位到工作流设计器中的相应活动,并且该大纲视图将会更新以显示在设计器中选择的活动。Clicking on a node in outline view will navigate to the corresponding activity in the workflow designer, and the outline view will be updated to show activities that are selected in the designer.

入门教程中的已完成工作流的以下屏幕截图显示了包含顺序工作流的大纲视图。The following screenshot of the completed workflow from the Getting Started Tutorial shows the outline view with a sequential workflow.

使用 Visual Studio 中的顺序工作流的大纲视图的屏幕截图。

C # 表达式C# Expressions

在 .NET Framework 4.5 之前,工作流中的所有表达式只能用 Visual Basic 编写。Prior to .NET Framework 4.5, all expressions in workflows could only be written in Visual Basic. 在 .NET Framework 4.5 中,Visual Basic 表达式仅用于使用 Visual Basic 创建的项目。In .NET Framework 4.5, Visual Basic expressions are only used for projects created using Visual Basic. Visual C# 项目现在将 C# 用于表达式。Visual C# projects now use C# for expressions. 提供了功能完全的 C# 表达式编辑器,包括语法突出显示和 Intellisense 等功能。A fully functional C# expression editor is provided which capabilities such as grammar highlighting and intellisense. 在使用 Visual Basic 表达式的以前版本中创建的 C# 工作流项目仍可继续使用。C# workflow projects created in previous versions that use Visual Basic expressions will continue to work.

将在设计时对 C# 表达式进行验证。C# expressions are validated at design-time. C# 表达式中的错误将标有红色波浪形下划线。Errors in C# expressions will be marked with a red wavy underline.

有关 c # 表达式的详细信息,请参阅c # 表达式For more information about C# expressions, see C# Expressions.

更好地控制 shell 栏和标题项的可见性More control of visibility of shell bar and header items

在重新承载的设计器中,某些标准 UI 控件可能对于给定的工作流没有意义,并可能已关闭。In a rehosted designer, some of the standard UI controls may not have meaning for a given workflow, and may be turned off. 在 .NET Framework 4 中,仅在设计器底部的 shell 栏支持此自定义项。In .NET Framework 4, this customization is only supported by the shell bar at the bottom of the designer. 在 .NET Framework 4.5 中,可以通过设置适当的值来调整设计器顶部 shell 标头项的可见性 WorkflowShellHeaderItemsVisibility ShellHeaderItemsVisibilityIn .NET Framework 4.5, the visibility of shell header items at the top of the designer can be adjusted by setting WorkflowShellHeaderItemsVisibility with the appropriate ShellHeaderItemsVisibility value.

流程图和状态机工作流中的自动连接和自动插入Auto-connect and auto-insert in Flowchart and State Machine workflows

在 .NET Framework 4 中,必须手动添加流程图工作流中节点之间的连接。In .NET Framework 4, connections between nodes in a Flowchart workflow had to be added manually. 在 .NET Framework 4.5 中,流程图和状态机节点具有在将活动从工具箱拖到设计器图面时变为可见的自动连接点。In .NET Framework 4.5, Flowchart and State Machine nodes have auto-connect points that become visible when an activity is dragged from the toolbox onto the designer surface. 将活动放置在这些点中的一个点上会自动添加该活动以及必要的连接。Dropping an activity on one of these points automatically adds the activity along with the necessary connection.

下面的屏幕快照显示了从工具箱中拖动活动时变为可见的附属点。The following screenshot shows the attachment points that become visible when an activity is dragged from the toolbox.

显示自动连接点的流程图开始节点

也可以将活动拖动到流程图节点和状态之间的连接上,以在两个其他节点之间自动插入该节点。Activities can also be dragged onto connections between flowchart nodes and states to auto-insert the node between two other nodes. 以下屏幕快照显示了突出显示的连接线,可在此连接线处从工具箱拖动并放置活动。The following screenshot shows the highlighted connecting line where activities can be dragged from the toolbox and dropped.

放置活动的自动插入处理

设计器批注Designer Annotations

为促进开发较大型的工作流,设计器现在支持添加批注以帮助跟踪设计过程。To facilitate developing larger workflows, the designer now supports adding annotations to help keep track of the design process. 可以向活动、状态、流程图节点、变量和自变量添加批注。Annotation can be added to activities, states, flowchart nodes, variables and arguments. 以下屏幕快照显示了用于将批注添加到设计器的上下文菜单。The following screenshot shows the context menu used to add annotations to the designer.

显示用于添加批注的菜单的屏幕截图。

调试状态Debugging states

在 .NET Framework 4 中,非活动元素不支持调试断点,因为它们不是执行单元。In .NET Framework 4, non-activity elements could not support debug breakpoints since they were not units of execution. 此版本提供了一种向 State 对象添加断点的机制。This release provides a mechanism for adding breakpoints to State objects. 若在 State 上设置了断点,则执行将在计划了状态入口活动或触发器之前、状态发生转换时中断。When a breakpoint is set on a State, execution will break when the state is transitioned to, before its entry activities or triggers are scheduled.

在设计器中定义和使用 ActivityDelegate 对象Define and consume ActivityDelegate objects in the designer

.NET Framework 4 中的活动使用 ActivityDelegate 对象公开工作流的其他部分可与工作流的执行交互的执行点,但使用这些执行点通常需要大量的代码。Activities in .NET Framework 4 used ActivityDelegate objects to expose execution points where other parts of the workflow could interact with a workflow's execution, but using these execution points usually required a fair amount of code. 在此版本中,开发人员可以使用工作流设计器来定义和使用活动委托。In this release, developers can define and consume activity delegates using the workflow designer. 有关详细信息,请参阅如何:在工作流设计器中定义和使用活动委托For more information, see How to: Define and consume activity delegates in the Workflow Designer.

生成时验证Build-time validation

在 .NET Framework 4 中,工作流验证错误在工作流项目的生成过程中不计为生成错误。In .NET Framework 4, workflow validation errors weren’t counted as build errors during the build of a workflow project. 这意味着即使存在工作流验证错误,工作流项目的生成也可能成功。This meant that building a workflow project could succeed even when there were workflow validation errors. 在 .NET Framework 4.5 中,工作流验证错误将导致生成失败。In .NET Framework 4.5, workflow validation errors cause the build to fail.

设计时后台验证Design-time background validation

在 .NET Framework 4 中,工作流被验证为前台进程,这可能会在复杂或耗时的验证过程中阻止 UI。In .NET Framework 4, workflows were validated as a foreground process, which could potentially block the UI during complex or time-consuming validation processes. 工作流验证现在发生在后台线程上,因此不会阻止 UI。Workflow validation now takes place on a background thread, so that the UI is not blocked.

位于 XAML 文件中单独位置的视图状态View state located in a separate location in XAML files

在 .NET Framework 4 中,工作流的视图状态信息存储在多个不同位置的 XAML 文件中。In .NET Framework 4, the view state information for a workflow is stored across the XAML file in many different locations. 这对于想直接读取 XAML 或编写用于删除视图状态信息的代码的开发人员来说很不方便。This is inconvenient for developers who want to read XAML directly, or write code to remove the view state information. 在 .NET Framework 4.5 中,XAML 文件中的视图状态信息序列化为 XAML 文件中的单独元素。In .NET Framework 4.5, the view state information in the XAML file is serialized as a separate element in the XAML file. 开发人员可以轻松地查找和编辑活动的视图状态信息,或者完全删除视图状态。Developers can easily locate and edit the view state information of an activity, or remove the view state altogether.

表达式扩展性Expression extensibility

在 .NET Framework 4.5 中,我们为开发人员提供了一种方法来创建可插入工作流设计器中的表达式和表达式创作体验。In .NET Framework 4.5, we provide a way for developers to create their own expression and expression authoring experience that can be plugged into the workflow designer.

在重新承载设计器中选择加入工作流4.5 功能Opt-in for Workflow 4.5 features in rehosted designer

为了保持向后兼容性,默认情况下,重新承载设计器中不会启用 .NET Framework 4.5 中包含的一些新功能。To preserve backward compatibility, some new features included in .NET Framework 4.5 are not enabled by default in the rehosted designer. 这是为了确保使用重新承载的设计器的现有应用程序不会由于更新至最新版本而中断。This is to ensure that existing applications that use the rehosted designer are not broken by updating to the latest version. 若要启用重新承载的设计器中的新功能,可将 TargetFrameworkName 设置为“.NET Framework 4.5”,或设置 DesignerConfigurationService 的各成员以启用各个功能。To enable new features in the rehosted designer, either set TargetFrameworkName to ".NET Framework 4.5", or set individual members of DesignerConfigurationService to enable individual features.

新工作流开发模型New Workflow Development Models

除流程图和顺序工作流开发模型外,此版本还包括状态机工作流和协定优先工作流服务。In addition to flowchart and sequential workflow development models, this release includes State Machine workflows, and contract-first workflow services.

状态机工作流State machine workflows

状态机工作流是在Microsoft .NET Framework 4 平台更新 1中作为 .NET Framework 4 版本4.0.1 的一部分引入的。State machine workflows were introduced as part of the .NET Framework 4, version 4.0.1 in the Microsoft .NET Framework 4 Platform Update 1. 此更新包括多个新类和活动,允许开发人员创建状态机工作流。This update included several new classes and activities which allowed developers to create state machine workflows. 这些类和活动已针对 .NET Framework 4.5 进行了更新。These classes and activities have been updated for .NET Framework 4.5. 更新包括:Updates include:

  1. 能够对状态设置断点The ability to set breakpoints on states

  2. 能够在工作流设计器中复制和粘贴转换The ability to copy and paste transitions in the workflow designer

  3. 设计器支持共享触发器转换创建Designer support for shared trigger transition creation

  4. 用于创建状态机工作流的活动,包括:StateMachineStateTransitionActivities used to create State Machine workflows, including: StateMachine, State, and Transition

以下屏幕截图显示了入门教程步骤如何:创建状态机工作流中的已完成状态机工作流。The following screenshot shows the completed state machine workflow from the Getting Started Tutorial step How to: Create a State Machine Workflow.

显示已完成状态机工作流的插图。

有关创建状态机工作流的详细信息,请参阅状态机工作流For more information on creating state machine workflows, see State Machine Workflows.

协定优先工作流开发Contract-first workflow development

协定优先工作流开发工具允许开发人员首先在代码中设计协定,然后,只需在 Visual Studio 中单击几下鼠标,就会在工具箱中自动生成表示每个操作的活动模板。The contract-first workflow development tool allows the developer to design a contract in code first, then, with a few clicks in Visual Studio, automatically generate an activity template in the toolbox representing each operation. 这些活动随后用于创建实现由该协定定义的操作的工作流。These activities are then used to create a workflow that implements the operations defined by the contract. 工作流设计器将对该工作流服务进行验证,以确保实现这些操作并且工作流的签名与协定签名匹配。The workflow designer will validate the workflow service to ensure that these operations are implemented and the signature of the workflow matches the contract signature. 开发人员还可以将工作流服务与所实现的协定的集合进行关联。The developer can also associate a workflow service with a collection of implemented contracts. 有关协定优先工作流服务开发的详细信息,请参阅如何:创建使用现有服务协定的工作流服务For more information on contract-first workflow service development, see How to: Create a workflow service that consumes an existing service contract.