WF4.0 Beta1 持久化 (SqlPersistenceProviderFactory)
SqlPersistenceProviderFactory与PersistenceProvider
名称空间: System.ServiceModel.Persistence
类文件: System.WorkflowServices.dll
数据库脚本: [\Windows\Microsoft.NET\Framework\v4.0.20506\SQL\en] 目录中
功能类
public class wfTools { //System.ServiceModel.Persistence //System.WorkflowServices.dll static SqlPersistenceProviderFactory persistenceProviderFactory;
public static void openPersistence() { persistenceProviderFactory =new SqlPersistenceProviderFactory ( @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\persistence.mdf;Integrated Security=True", false, false, TimeSpan.FromSeconds(60) ); persistenceProviderFactory.Open(); }
public static void closePersistence() { persistenceProviderFactory.Close(); }
public static WorkflowInstance createWorkflow(WorkflowElement workflow, Guid id) {
WorkflowInstance instance = new WorkflowInstance(workflow, id);
bindingInstanceEvent(instance);
PersistenceProvider pp = persistenceProviderFactory.CreateProvider(id); instance.Extensions.Add(pp);
return instance; }
public static WorkflowInstance loadWorkflow(WorkflowElement workflow, Guid id) { PersistenceProvider pp = persistenceProviderFactory.CreateProvider(id); WorkflowInstance instance = WorkflowInstance.Load(workflow, pp); bindingInstanceEvent(instance); return instance; }
static void bindingInstanceEvent(WorkflowInstance instance) {
instance.OnIdle = idel; instance.OnUnloaded = unloaded; instance.OnUnhandledException = unhandledException; instance.OnCompleted = completed; }
static void unloaded() { System.Console.WriteLine("unloaded"); }
static IdleAction idel() { System.Console.WriteLine("idel"); return IdleAction.Unload; }
static void completed(WorkflowCompletedEventArgs e) { System.Console.WriteLine("completed"); }
static void aborted(WorkflowAbortedEventArgs e) { System.Console.WriteLine("aborted", e.Reason.Message); }
static UnhandledExceptionAction unhandledException(WorkflowUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException{0}", e.UnhandledException.Message);
return UnhandledExceptionAction.Abort; }
} |
例子流程
等待触发 | public class myBookmark : NativeActivity { public string bookmarkName { get; set; } protected override void Execute(ActivityExecutionContext context) { context.CreateNamedBookmark(bookmarkName); } } |
流程 |
使用
第一组代码 | Guid id = new Guid("DB35221A-C901-46F7-8075-E65A8D97FE13"); wfTools.openPersistence();
WorkflowInstance myInstance = wfTools.createWorkflow(new Sequence1(), id); myInstance.Run();
System.Console.Read();
|
第二组代码 | Guid id = new Guid("DB35221A-C901-46F7-8075-E65A8D97FE13"); wfTools.openPersistence();
WorkflowInstance myInstance = wfTools.loadWorkflow(new Sequence1(), id); myInstance.ResumeBookmark("wxd", null);
System.Console.Read();
|