WorkflowIdentity 为工作流应用程序开发人员提供了一种将名称和版本与工作流定义关联的方法,这种方法还可用于将此信息与持久化工作流实例相关联。 工作流应用程序开发人员可以使用这些标识信息,为一些情景(如并行执行一个工作流定义的多个版本)提供支持,并为其他功能(如动态更新)提供基础。 该教程中的此步骤演示了如何使用 WorkflowIdentity 来同时承载多个版本的工作流。
Note: 若要下载完整的版本或观看教程视频演练,请参阅Windows Workflow Foundation (WF45)-入门教程。
在本主题中
在该教程的此步骤中,工作流中的 WriteLine 活动已得到修改以提供其他信息,并且添加了一个新的 WriteLine 活动。 将存储原始工作流程序集的副本,并更新主机应用程序以便其可同时在原始工作流和更新后的工作流中运行。
Note: 在执行本主题中的步骤之前,请运行该应用程序,启动每个类型的多个工作流,并针对每个工作流进行一个或两个猜测。 此步骤和下一步中使用这些持久化工作流如何:更新正在运行的工作流实例的定义。
Note: 入门教程中的每个步骤都依赖于前面的步骤。 如果未完成前面的步骤可以下载已完成的版本的教程Windows Workflow Foundation (WF45)-入门教程。
制作 NumberGuessWorkflowActivities 项目的副本
-
打开WF45GettingStartedTutorial如果打开的 Visual Studio 2012 中的解决方案。
-
按 Ctrl+Shift+B 生成解决方案。
-
关闭WF45GettingStartedTutorial解决方案。
-
打开 Windows 资源管理器并定位到该教程解决方案文件和项目文件夹所在的文件夹。
-
创建一个名为的新文件夹PreviousVersions所在的同一文件夹中NumberGuessWorkflowHost并NumberGuessWorkflowActivities。 此文件夹用于放置包含在后续教程步骤中使用的不同版本工作流的程序集。
-
导航到numberguessworkflowactivities\bin\debug文件夹 (或bin\release取决于项目设置)。 复制numberguessworkflowactivities.dll将其粘贴到PreviousVersions文件夹。
-
重命名numberguessworkflowactivities.dll中PreviousVersions文件夹NumberGuessWorkflowActivities_v1.dll。
NOTE 本主题中的步骤演示了一种对用于包含多个版本的工作流的程序集进行管理的方法。 也可以使用其他方法,例如,对程序集进行强命名,并在全局程序集缓冲中注册这些程序集。
-
创建一个名为的新文件夹NumberGuessWorkflowActivities_du所在的同一文件夹中NumberGuessWorkflowHost, NumberGuessWorkflowActivities,和新添加PreviousVersions文件夹,并将所有文件和子文件夹中的复制NumberGuessWorkflowActivities到新文件夹NumberGuessWorkflowActivities_du文件夹。 在使用活动的初始版本的项目的此备份副本如何:更新正在运行的工作流实例的定义。
-
重新打开WF45GettingStartedTutorial Visual Studio 2012 中的解决方案。
更新工作流
在本节中,将会更新工作流定义。 将会更新针对用户猜测提供反馈的两个 WriteLine 活动,并添加一个新的 WriteLine 活动,此活动提供猜测了数字之后有关该游戏的其他信息。
若要更新的状态机工作流
-
在中解决方案资源管理器下NumberGuessWorkflowActivities项目中,双击StateMachineNumberGuessWorkflow.xaml。
-
双击Guess Incorrect状态机上的转换。
-
更新
Text活动中最左侧WriteLine的If。Guess & " is too low."Guess + " is too low." -
更新
Text活动中最右侧WriteLine的If。Guess & " is too high."Guess + " is too high." -
返回到整体状态机工作流设计器中的视图,通过单击StateMachine痕迹导航中显示在工作流设计器的顶部。
-
双击Guess Correct状态机上的转换。
-
拖动WriteLine活动从基元一部分工具箱并将其放置在此处放置操作活动的标签转换。
-
在
Text属性框中键入以下表达式。Guess & " is correct. You guessed it in " & Turns & " turns."Guess + " is correct. You guessed it in " + Turns + " turns."
若要更新流程图工作流
-
在中解决方案资源管理器下NumberGuessWorkflowActivities项目中,双击FlowchartNumberGuessWorkflow.xaml。
-
更新最左侧
Text活动的WriteLine。Guess & " is too low."Guess + " is too low." -
更新最右侧
Text活动的WriteLine。Guess & " is too high."Guess + " is too high." -
拖动WriteLine活动从基元一部分工具箱并将其放的放置点上
True操作的最上面FlowDecision.WriteLine活动将添加到流程图并链接到True的FlowDecision操作。 -
在
Text属性框中键入以下表达式。Guess & " is correct. You guessed it in " & Turns & " turns."Guess + " is correct. You guessed it in " + Turns + " turns."
若要更新的顺序工作流
-
在中解决方案资源管理器下NumberGuessWorkflowActivities项目中,双击SequentialNumberGuessWorkflow.xaml。
-
更新
Text活动中最左侧WriteLine的If。Guess & " is too low."Guess + " is too low." -
更新
Text活动中最右侧WriteLine活动的If。Guess & " is too high."Guess + " is too high." -
拖动WriteLine活动从基元一部分工具箱然后将其放置DoWhile活动,以便WriteLine是根目录中的最后一个活动
Sequence活动。 -
在
Text属性框中键入以下表达式。Guess & " is correct. You guessed it in " & Turns & " turns."Guess + " is correct. You guessed it in " + Turns + " turns."
若要更新 WorkflowVersionMap 以包括以前的工作流版本
-
双击单击(或WorkflowVersionMap.vb) 下NumberGuessWorkflowHost项目以打开它。
-
向包含其他
using(或Imports)语句的文件的顶部添加以下using(或Imports)语句。Imports System.Reflection Imports System.IOusing System.Reflection; using System.IO; -
紧接在现有的三个工作流标识声明下面添加三个新工作流标识。 这些新的
v1工作流标识用于为在进行更新前启动的工作流提供正确的工作流定义。'Current version identities. Public StateMachineNumberGuessIdentity As WorkflowIdentity Public FlowchartNumberGuessIdentity As WorkflowIdentity Public SequentialNumberGuessIdentity As WorkflowIdentity 'v1 Identities. Public StateMachineNumberGuessIdentity_v1 As WorkflowIdentity Public FlowchartNumberGuessIdentity_v1 As WorkflowIdentity Public SequentialNumberGuessIdentity_v1 As WorkflowIdentity// Current version identities. static public WorkflowIdentity StateMachineNumberGuessIdentity; static public WorkflowIdentity FlowchartNumberGuessIdentity; static public WorkflowIdentity SequentialNumberGuessIdentity; // v1 identities. static public WorkflowIdentity StateMachineNumberGuessIdentity_v1; static public WorkflowIdentity FlowchartNumberGuessIdentity_v1; static public WorkflowIdentity SequentialNumberGuessIdentity_v1; -
在
WorkflowVersionMap构造函数中,将三个当前工作流标识的Version属性更新为2.0.0.0。'Add the current workflow version identities. StateMachineNumberGuessIdentity = New WorkflowIdentity With { .Name = "StateMachineNumberGuessWorkflow", .Version = New Version(2, 0, 0, 0) } FlowchartNumberGuessIdentity = New WorkflowIdentity With { .Name = "FlowchartNumberGuessWorkflow", .Version = New Version(2, 0, 0, 0) } SequentialNumberGuessIdentity = New WorkflowIdentity With { .Name = "SequentialNumberGuessWorkflow", .Version = New Version(2, 0, 0, 0) } map.Add(StateMachineNumberGuessIdentity, New StateMachineNumberGuessWorkflow()) map.Add(FlowchartNumberGuessIdentity, New FlowchartNumberGuessWorkflow()) map.Add(SequentialNumberGuessIdentity, New SequentialNumberGuessWorkflow())// Add the current workflow version identities. StateMachineNumberGuessIdentity = new WorkflowIdentity { Name = "StateMachineNumberGuessWorkflow", // Version = new Version(1, 0, 0, 0), Version = new Version(2, 0, 0, 0) }; FlowchartNumberGuessIdentity = new WorkflowIdentity { Name = "FlowchartNumberGuessWorkflow", // Version = new Version(1, 0, 0, 0), Version = new Version(2, 0, 0, 0) }; SequentialNumberGuessIdentity = new WorkflowIdentity { Name = "SequentialNumberGuessWorkflow", // Version = new Version(1, 0, 0, 0), Version = new Version(2, 0, 0, 0) }; map.Add(StateMachineNumberGuessIdentity, new StateMachineNumberGuessWorkflow()); map.Add(FlowchartNumberGuessIdentity, new FlowchartNumberGuessWorkflow()); map.Add(SequentialNumberGuessIdentity, new SequentialNumberGuessWorkflow());向字典添加当前版本的工作流的代码使用项目中引用的当前版本,因此,无需对初始化工作流定义的代码进行更新。
-
紧接在向字典添加当前版本的代码的后面添加以下代码。
'Initialize the previous workflow version identities. StateMachineNumberGuessIdentity_v1 = New WorkflowIdentity With { .Name = "StateMachineNumberGuessWorkflow", .Version = New Version(1, 0, 0, 0) } FlowchartNumberGuessIdentity_v1 = New WorkflowIdentity With { .Name = "FlowchartNumberGuessWorkflow", .Version = New Version(1, 0, 0, 0) } SequentialNumberGuessIdentity_v1 = New WorkflowIdentity With { .Name = "SequentialNumberGuessWorkflow", .Version = New Version(1, 0, 0, 0) }// Initialize the previous workflow version identities. StateMachineNumberGuessIdentity_v1 = new WorkflowIdentity { Name = "StateMachineNumberGuessWorkflow", Version = new Version(1, 0, 0, 0) }; FlowchartNumberGuessIdentity_v1 = new WorkflowIdentity { Name = "FlowchartNumberGuessWorkflow", Version = new Version(1, 0, 0, 0) }; SequentialNumberGuessIdentity_v1 = new WorkflowIdentity { Name = "SequentialNumberGuessWorkflow", Version = new Version(1, 0, 0, 0) };这些工作流标识与相应工作流定义的初始版本相关联。
-
接下来,加载包含初始版本的工作流定义的程序集,然后创建相应工作流定义,并将这些定义添加到字典。
'Add the previous version workflow identities to the dictionary along with 'the corresponding workflow definitions loaded from the v1 assembly. 'Assembly.LoadFile requires an absolute path so convert this relative path 'to an absolute path. Dim v1AssemblyPath As String = "..\..\..\PreviousVersions\NumberGuessWorkflowActivities_v1.dll" v1AssemblyPath = Path.GetFullPath(v1AssemblyPath) Dim v1Assembly As Assembly = Assembly.LoadFile(v1AssemblyPath) map.Add(StateMachineNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.StateMachineNumberGuessWorkflow")) map.Add(SequentialNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.SequentialNumberGuessWorkflow")) map.Add(FlowchartNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.FlowchartNumberGuessWorkflow"))// Add the previous version workflow identities to the dictionary along with // the corresponding workflow definitions loaded from the v1 assembly. // Assembly.LoadFile requires an absolute path so convert this relative path // to an absolute path. string v1AssemblyPath = @"..\..\..\PreviousVersions\NumberGuessWorkflowActivities_v1.dll"; v1AssemblyPath = Path.GetFullPath(v1AssemblyPath); Assembly v1Assembly = Assembly.LoadFile(v1AssemblyPath); map.Add(StateMachineNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.StateMachineNumberGuessWorkflow") as Activity); map.Add(SequentialNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.SequentialNumberGuessWorkflow") as Activity); map.Add(FlowchartNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.FlowchartNumberGuessWorkflow") as Activity);下面的示例完整列出了已更新的
WorkflowVersionMap类。Public Module WorkflowVersionMap Dim map As Dictionary(Of WorkflowIdentity, Activity) 'Current version identities. Public StateMachineNumberGuessIdentity As WorkflowIdentity Public FlowchartNumberGuessIdentity As WorkflowIdentity Public SequentialNumberGuessIdentity As WorkflowIdentity 'v1 Identities. Public StateMachineNumberGuessIdentity_v1 As WorkflowIdentity Public FlowchartNumberGuessIdentity_v1 As WorkflowIdentity Public SequentialNumberGuessIdentity_v1 As WorkflowIdentity Sub New() map = New Dictionary(Of WorkflowIdentity, Activity) 'Add the current workflow version identities. StateMachineNumberGuessIdentity = New WorkflowIdentity With { .Name = "StateMachineNumberGuessWorkflow", .Version = New Version(2, 0, 0, 0) } FlowchartNumberGuessIdentity = New WorkflowIdentity With { .Name = "FlowchartNumberGuessWorkflow", .Version = New Version(2, 0, 0, 0) } SequentialNumberGuessIdentity = New WorkflowIdentity With { .Name = "SequentialNumberGuessWorkflow", .Version = New Version(2, 0, 0, 0) } map.Add(StateMachineNumberGuessIdentity, New StateMachineNumberGuessWorkflow()) map.Add(FlowchartNumberGuessIdentity, New FlowchartNumberGuessWorkflow()) map.Add(SequentialNumberGuessIdentity, New SequentialNumberGuessWorkflow()) 'Initialize the previous workflow version identities. StateMachineNumberGuessIdentity_v1 = New WorkflowIdentity With { .Name = "StateMachineNumberGuessWorkflow", .Version = New Version(1, 0, 0, 0) } FlowchartNumberGuessIdentity_v1 = New WorkflowIdentity With { .Name = "FlowchartNumberGuessWorkflow", .Version = New Version(1, 0, 0, 0) } SequentialNumberGuessIdentity_v1 = New WorkflowIdentity With { .Name = "SequentialNumberGuessWorkflow", .Version = New Version(1, 0, 0, 0) } 'Add the previous version workflow identities to the dictionary along with 'the corresponding workflow definitions loaded from the v1 assembly. 'Assembly.LoadFile requires an absolute path so convert this relative path 'to an absolute path. Dim v1AssemblyPath As String = "..\..\..\PreviousVersions\NumberGuessWorkflowActivities_v1.dll" v1AssemblyPath = Path.GetFullPath(v1AssemblyPath) Dim v1Assembly As Assembly = Assembly.LoadFile(v1AssemblyPath) map.Add(StateMachineNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.StateMachineNumberGuessWorkflow")) map.Add(SequentialNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.SequentialNumberGuessWorkflow")) map.Add(FlowchartNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.FlowchartNumberGuessWorkflow")) End Sub Public Function GetWorkflowDefinition(identity As WorkflowIdentity) As Activity Return map(identity) End Function Public Function GetIdentityDescription(identity As WorkflowIdentity) As String Return identity.ToString() End Function End Modulepublic static class WorkflowVersionMap { static Dictionary<WorkflowIdentity, Activity> map; // Current version identities. static public WorkflowIdentity StateMachineNumberGuessIdentity; static public WorkflowIdentity FlowchartNumberGuessIdentity; static public WorkflowIdentity SequentialNumberGuessIdentity; // v1 identities. static public WorkflowIdentity StateMachineNumberGuessIdentity_v1; static public WorkflowIdentity FlowchartNumberGuessIdentity_v1; static public WorkflowIdentity SequentialNumberGuessIdentity_v1; static WorkflowVersionMap() { map = new Dictionary<WorkflowIdentity, Activity>(); // Add the current workflow version identities. StateMachineNumberGuessIdentity = new WorkflowIdentity { Name = "StateMachineNumberGuessWorkflow", // Version = new Version(1, 0, 0, 0), Version = new Version(2, 0, 0, 0) }; FlowchartNumberGuessIdentity = new WorkflowIdentity { Name = "FlowchartNumberGuessWorkflow", // Version = new Version(1, 0, 0, 0), Version = new Version(2, 0, 0, 0) }; SequentialNumberGuessIdentity = new WorkflowIdentity { Name = "SequentialNumberGuessWorkflow", // Version = new Version(1, 0, 0, 0), Version = new Version(2, 0, 0, 0) }; map.Add(StateMachineNumberGuessIdentity, new StateMachineNumberGuessWorkflow()); map.Add(FlowchartNumberGuessIdentity, new FlowchartNumberGuessWorkflow()); map.Add(SequentialNumberGuessIdentity, new SequentialNumberGuessWorkflow()); // Initialize the previous workflow version identities. StateMachineNumberGuessIdentity_v1 = new WorkflowIdentity { Name = "StateMachineNumberGuessWorkflow", Version = new Version(1, 0, 0, 0) }; FlowchartNumberGuessIdentity_v1 = new WorkflowIdentity { Name = "FlowchartNumberGuessWorkflow", Version = new Version(1, 0, 0, 0) }; SequentialNumberGuessIdentity_v1 = new WorkflowIdentity { Name = "SequentialNumberGuessWorkflow", Version = new Version(1, 0, 0, 0) }; // Add the previous version workflow identities to the dictionary along with // the corresponding workflow definitions loaded from the v1 assembly. // Assembly.LoadFile requires an absolute path so convert this relative path // to an absolute path. string v1AssemblyPath = @"..\..\..\PreviousVersions\NumberGuessWorkflowActivities_v1.dll"; v1AssemblyPath = Path.GetFullPath(v1AssemblyPath); Assembly v1Assembly = Assembly.LoadFile(v1AssemblyPath); map.Add(StateMachineNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.StateMachineNumberGuessWorkflow") as Activity); map.Add(SequentialNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.SequentialNumberGuessWorkflow") as Activity); map.Add(FlowchartNumberGuessIdentity_v1, v1Assembly.CreateInstance("NumberGuessWorkflowActivities.FlowchartNumberGuessWorkflow") as Activity); } public static Activity GetWorkflowDefinition(WorkflowIdentity identity) { return map[identity]; } public static string GetIdentityDescription(WorkflowIdentity identity) { return identity.ToString(); } }
生成并运行应用程序
-
按 Ctrl+Shift+B 以生成应用程序,然后按 Ctrl+F5 启动该应用程序。
-
通过单击启动新工作流新游戏。 工作流的版本显示在状态窗口下面,它反映了关联
WorkflowIdentity的更新版本。 请记下InstanceId,这样您就可以在工作流完成时查看该工作流的跟踪文件,然后在游戏完成之前输入猜测。 请注意用户猜测基于WriteLine活动的更新在状态窗口中的信息中显示的方式。Please enter a number between 1 and 10 5 is too high. Please enter a number between 1 and 10 3 is too high. Please enter a number between 1 and 10 1 is too low. Please enter a number between 1 and 10 Congratulations, you guessed the number in 4 turns.NOTE 将显示
WriteLine活动的更新文本,但不显示已在本主题中添加的最终WriteLine活动的输出。 这是因为,此状态窗口将由PersistableIdle处理程序进行更新。 由于该工作流在最终活动之后完成且不会转为空闲状态,因此不会调用PersistableIdle处理程序。 但是,Completed处理程序会在状态窗口中显示类似的消息。 如果需要,可以向Completed处理程序添加代码,以便从StringWriter提取文本并在状态窗口中显示。 -
打开 Windows 资源管理器并导航到NumberGuessWorkflowHost\bin\debug文件夹 (或bin\release取决于项目设置) 和跟踪使用记事本打开文件相对应为完成的工作流。 如果未进行记
InstanceId,可以通过使用标识正确的跟踪文件修改日期Windows 资源管理器中的信息。Please enter a number between 1 and 10 5 is too high. Please enter a number between 1 and 10 3 is too high. Please enter a number between 1 and 10 1 is too low. Please enter a number between 1 and 10 2 is correct. You guessed it in 4 turns.该跟踪文件中包含已更新的
WriteLine输出,包括在本主题中添加的WriteLine的输出。 -
切换回数字猜测应用程序,然后选择在进行更新前启动的一个工作流。 您可通过查看在状态窗口下面显示的版本信息来识别当前选择的工作流的版本。 输入一些猜测,并注意状态更新与前一个版本的
WriteLine活动输出相符,并且不包括用户猜测。 这是因为,这些工作流使用的是不具有WriteLine更新的前一个工作流定义。在下一步,如何:更新运行工作流实例的定义,运行
v1工作流实例会更新,因此,它们包含的新功能的方式v2实例。
历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体、发布器占位标签和导航标记。
