自定义PersistenceService类
该例自定义了一个将实例持久化到磁盘文的操作,分两部分
第一部分是实现PersistenceService类,第二部分是操作磁盘的功能类,本例中的[test]是一个自定义的console的输出类,功能就是实现console.write("内容")
实现PersistenceService类
操作磁盘的功能类
然后象加载SqlWorkflowPersistenceService类一样使用就可以了
第一部分是实现PersistenceService类,第二部分是操作磁盘的功能类,本例中的[test]是一个自定义的console的输出类,功能就是实现console.write("内容")
实现PersistenceService类
Imports System
Imports System.Workflow.ComponentModel
Imports System.Workflow.Runtime.Hosting
Imports System.Collections.Specialized
Imports System.Workflow.Runtime
Public Class 自定义Persistence服务
Inherits WorkflowPersistenceService
'当实例进入Idle状态时,引擎是否自动调用unload
'为True 时,当实例进入Idle状态时,引擎自动调用实例的unload方法,
'具体见 SqlWorkflowPersistenceService类 的
Private unloadOnIdleValue As Boolean = False
Public Sub New()
'注本服务默认为进入Idle状态的的实例不被引擎自动调用unload方法
End Sub
Public Sub New(ByVal unloadOnIdle As Boolean)
'跟据传入的参数,操作当前实例的操作模式值:unloadOnIdleValue
unloadOnIdleValue = unloadOnIdle
End Sub
' 载入已完成的activity
Protected Overrides Function LoadCompletedContextActivity(ByVal scopeId As System.Guid, ByVal outerActivity As System.Workflow.ComponentModel.Activity) As System.Workflow.ComponentModel.Activity
'有个 out 参数 outerActivity
Dim a As New System.Collections.ArrayList
a.Add("实例:" + scopeId.ToString)
test.持久化("准备:载入已完成的activity:LoadCompletedContextActivity", a)
Dim obj As Object = 磁盘文件操作.加载(scopeId, outerActivity)
Return CType(obj, Activity)
End Function
' 载入实例
Protected Overrides Function LoadWorkflowInstanceState(ByVal instanceId As System.Guid) As System.Workflow.ComponentModel.Activity
Dim a As New System.Collections.ArrayList
a.Add("实例:" + instanceId.ToString)
test.持久化("准备:载入实例:LoadWorkflowInstanceState", a)
Dim obj As Object = 磁盘文件操作.加载(instanceId, Nothing)
Return CType(obj, Activity)
End Function
'保存已完成的状态
Protected Overrides Sub SaveCompletedContextActivity(ByVal activity As System.Workflow.ComponentModel.Activity)
Dim contextGuid As Guid = CType(activity.GetValue(activity.ActivityContextGuidProperty), Guid)
Dim a As New System.Collections.ArrayList
a.Add("实例:" + contextGuid.ToString)
test.持久化("准备:保存已完成的状态:SaveCompletedContextActivity", a)
磁盘文件操作.保存(activity, contextGuid, True)
End Sub
' 保存实例
Protected Overrides Sub SaveWorkflowInstanceState(ByVal rootActivity As System.Workflow.ComponentModel.Activity, ByVal unlock As Boolean)
Dim contextGuid As Guid = CType(rootActivity.GetValue(Activity.ActivityContextGuidProperty), Guid)
Dim a As New System.Collections.ArrayList
a.Add("实例:" + contextGuid.ToString)
a.Add("unlock:" + unlock.ToString())
test.持久化("准备:保存实例:SaveWorkflowInstanceState", a)
磁盘文件操作.保存(rootActivity, contextGuid, unlock)
End Sub
'对锁定的实例解锁,使实例可被操作
Protected Overrides Sub UnlockWorkflowInstanceState(ByVal rootActivity As System.Workflow.ComponentModel.Activity)
Dim contextGuid As Guid = CType(rootActivity.GetValue(Activity.ActivityContextGuidProperty), Guid)
Dim a As New System.Collections.ArrayList
a.Add("被解锁的实例:" + contextGuid.ToString)
test.持久化("准备:对锁定的实例解锁:UnlockWorkflowInstanceState", a)
磁盘文件操作.Unlock(contextGuid)
End Sub
'返回对进入IDLE状态的实例的解决模式
Protected Overrides Function UnloadOnIdle(ByVal rootActivity As System.Workflow.ComponentModel.Activity) As Boolean
'为True 时,当实例进入Idle状态时,引擎自动调用实例的unload方法,
Dim contextGuid As Guid = CType(rootActivity.GetValue(Activity.ActivityContextGuidProperty), Guid)
Dim a As New System.Collections.ArrayList
a.Add("模式值:" + unloadOnIdleValue.ToString())
a.Add("实例:" + contextGuid.ToString())
test.持久化("返回对进入IDLE状态的实例的解决模式:UnloadOnIdle", a)
Return unloadOnIdleValue
End Function
End Class
Imports System.Workflow.ComponentModel
Imports System.Workflow.Runtime.Hosting
Imports System.Collections.Specialized
Imports System.Workflow.Runtime
Public Class 自定义Persistence服务
Inherits WorkflowPersistenceService
'当实例进入Idle状态时,引擎是否自动调用unload
'为True 时,当实例进入Idle状态时,引擎自动调用实例的unload方法,
'具体见 SqlWorkflowPersistenceService类 的
Private unloadOnIdleValue As Boolean = False
Public Sub New()
'注本服务默认为进入Idle状态的的实例不被引擎自动调用unload方法
End Sub
Public Sub New(ByVal unloadOnIdle As Boolean)
'跟据传入的参数,操作当前实例的操作模式值:unloadOnIdleValue
unloadOnIdleValue = unloadOnIdle
End Sub
' 载入已完成的activity
Protected Overrides Function LoadCompletedContextActivity(ByVal scopeId As System.Guid, ByVal outerActivity As System.Workflow.ComponentModel.Activity) As System.Workflow.ComponentModel.Activity
'有个 out 参数 outerActivity
Dim a As New System.Collections.ArrayList
a.Add("实例:" + scopeId.ToString)
test.持久化("准备:载入已完成的activity:LoadCompletedContextActivity", a)
Dim obj As Object = 磁盘文件操作.加载(scopeId, outerActivity)
Return CType(obj, Activity)
End Function
' 载入实例
Protected Overrides Function LoadWorkflowInstanceState(ByVal instanceId As System.Guid) As System.Workflow.ComponentModel.Activity
Dim a As New System.Collections.ArrayList
a.Add("实例:" + instanceId.ToString)
test.持久化("准备:载入实例:LoadWorkflowInstanceState", a)
Dim obj As Object = 磁盘文件操作.加载(instanceId, Nothing)
Return CType(obj, Activity)
End Function
'保存已完成的状态
Protected Overrides Sub SaveCompletedContextActivity(ByVal activity As System.Workflow.ComponentModel.Activity)
Dim contextGuid As Guid = CType(activity.GetValue(activity.ActivityContextGuidProperty), Guid)
Dim a As New System.Collections.ArrayList
a.Add("实例:" + contextGuid.ToString)
test.持久化("准备:保存已完成的状态:SaveCompletedContextActivity", a)
磁盘文件操作.保存(activity, contextGuid, True)
End Sub
' 保存实例
Protected Overrides Sub SaveWorkflowInstanceState(ByVal rootActivity As System.Workflow.ComponentModel.Activity, ByVal unlock As Boolean)
Dim contextGuid As Guid = CType(rootActivity.GetValue(Activity.ActivityContextGuidProperty), Guid)
Dim a As New System.Collections.ArrayList
a.Add("实例:" + contextGuid.ToString)
a.Add("unlock:" + unlock.ToString())
test.持久化("准备:保存实例:SaveWorkflowInstanceState", a)
磁盘文件操作.保存(rootActivity, contextGuid, unlock)
End Sub
'对锁定的实例解锁,使实例可被操作
Protected Overrides Sub UnlockWorkflowInstanceState(ByVal rootActivity As System.Workflow.ComponentModel.Activity)
Dim contextGuid As Guid = CType(rootActivity.GetValue(Activity.ActivityContextGuidProperty), Guid)
Dim a As New System.Collections.ArrayList
a.Add("被解锁的实例:" + contextGuid.ToString)
test.持久化("准备:对锁定的实例解锁:UnlockWorkflowInstanceState", a)
磁盘文件操作.Unlock(contextGuid)
End Sub
'返回对进入IDLE状态的实例的解决模式
Protected Overrides Function UnloadOnIdle(ByVal rootActivity As System.Workflow.ComponentModel.Activity) As Boolean
'为True 时,当实例进入Idle状态时,引擎自动调用实例的unload方法,
Dim contextGuid As Guid = CType(rootActivity.GetValue(Activity.ActivityContextGuidProperty), Guid)
Dim a As New System.Collections.ArrayList
a.Add("模式值:" + unloadOnIdleValue.ToString())
a.Add("实例:" + contextGuid.ToString())
test.持久化("返回对进入IDLE状态的实例的解决模式:UnloadOnIdle", a)
Return unloadOnIdleValue
End Function
End Class
操作磁盘的功能类
Imports System.IO
Imports System.IO.Compression
'该类是将工作流实例保存到磁盘、从磁盘上加载的实体操作类
Public Class 磁盘文件操作
Shared 流 As FileStream = Nothing
Shared 文件名 As String = Nothing
'保存activity实例状态到文件,
Public Shared Sub 保存(ByVal rootActivity As Activity, ByVal id As Guid, ByVal unlock As Boolean)
'unlock参数,是用来判断锁定的,本例中没用
文件名 = id.ToString() '以当前工作流的实例ID为文件名
Try
If File.Exists(文件名) Then File.Delete(文件名) '如果存在该文件,就删除
流 = New FileStream(文件名, FileMode.CreateNew, FileAccess.Write, FileShare.None) '新建流
rootActivity.Save(流) '将工作流实例写入流
Finally
If Not 流 Is Nothing Then
流.Close()
End If
End Try
test.持久化("磁盘文件操作.保存", "保存完成:" + 文件名)
End Sub
'按实例ID从文件解串实例状态
Public Shared Function 加载(ByVal id As Guid, ByVal outerActivity As Activity) As Object
文件名 = id.ToString()
Dim obj As Object = Nothing
Try
流 = New FileStream(文件名, FileMode.Open, FileAccess.Read, FileShare.Read) '从磁盘新建流
流.Seek(0, SeekOrigin.Begin) '从磁盘载入流
obj = Activity.Load(流, outerActivity)
test.持久化("磁盘文件操作.加载", "加载完成:" + 文件名)
Return obj
Finally
流.Close()
End Try
End Function
'该段代码用于实现对磁盘文件的解锁,如果上面提供了对磁盘文件的锁定操作
Public Shared Sub Unlock(ByVal id As Guid)
Try
文件名 = id.ToString()
流 = New FileStream(文件名, FileMode.Open)
File.SetAttributes(文件名, FileAttributes.Normal)
Finally
流.Close()
End Try
test.持久化("磁盘文件操作.Unlock", "解锁完成:" + 文件名)
End Sub
End Class
Imports System.IO.Compression
'该类是将工作流实例保存到磁盘、从磁盘上加载的实体操作类
Public Class 磁盘文件操作
Shared 流 As FileStream = Nothing
Shared 文件名 As String = Nothing
'保存activity实例状态到文件,
Public Shared Sub 保存(ByVal rootActivity As Activity, ByVal id As Guid, ByVal unlock As Boolean)
'unlock参数,是用来判断锁定的,本例中没用
文件名 = id.ToString() '以当前工作流的实例ID为文件名
Try
If File.Exists(文件名) Then File.Delete(文件名) '如果存在该文件,就删除
流 = New FileStream(文件名, FileMode.CreateNew, FileAccess.Write, FileShare.None) '新建流
rootActivity.Save(流) '将工作流实例写入流
Finally
If Not 流 Is Nothing Then
流.Close()
End If
End Try
test.持久化("磁盘文件操作.保存", "保存完成:" + 文件名)
End Sub
'按实例ID从文件解串实例状态
Public Shared Function 加载(ByVal id As Guid, ByVal outerActivity As Activity) As Object
文件名 = id.ToString()
Dim obj As Object = Nothing
Try
流 = New FileStream(文件名, FileMode.Open, FileAccess.Read, FileShare.Read) '从磁盘新建流
流.Seek(0, SeekOrigin.Begin) '从磁盘载入流
obj = Activity.Load(流, outerActivity)
test.持久化("磁盘文件操作.加载", "加载完成:" + 文件名)
Return obj
Finally
流.Close()
End Try
End Function
'该段代码用于实现对磁盘文件的解锁,如果上面提供了对磁盘文件的锁定操作
Public Shared Sub Unlock(ByVal id As Guid)
Try
文件名 = id.ToString()
流 = New FileStream(文件名, FileMode.Open)
File.SetAttributes(文件名, FileAttributes.Normal)
Finally
流.Close()
End Try
test.持久化("磁盘文件操作.Unlock", "解锁完成:" + 文件名)
End Sub
End Class
然后象加载SqlWorkflowPersistenceService类一样使用就可以了
Dim 引擎 As WorkflowRuntime = New WorkflowRuntime()
Dim 自定义PS As New 自定义Persistence服务(True) '为真时,引擎自动对进入idle模式的实例,调用Unload方法,
引擎.AddService(自定义PS)
Dim 自定义PS As New 自定义Persistence服务(True) '为真时,引擎自动对进入idle模式的实例,调用Unload方法,
引擎.AddService(自定义PS)