閱讀: 494 評論: 2 作者: 生魚片 發表于 2009-12-09 20:55 原文鏈接

WF4提供了強大的持久化模型,之前我也翻譯了一篇文章描述了WF4中持久化的一些知識,[譯]Windows Workflow Foundation 4 和持久性。

我會系統的介紹下WF4中的持久化服務。WF4提供了一個抽象類InstanceStrore,表示邏輯上的工作流實例的容器,還有一些和持久化相關的類如LoadWorkflowCommand,SaveWorkflowCommand等,具體的實現我們可以從InstanceStore類繼承來開發自己的Persistence ProviderWF4已經默認給我們實現好了一個基于SQL的持久化實現,對應于SqlWorkflowInstanceStore類。下面是這兩個類的繼承關系:

clip_image002

下面是關于SqlWorkflowInstanceStore類屬性的一些說明:

Instance Encoding Option:指定保存到持久化存儲的壓縮算法,可選值為GZipNone。

Instance Completion Action:指定當工作流實例完成后的操作(是否刪除持久化存儲中的數據),有兩個值DeleteAll(默認),DeleteNothing。

Instance Locked Exception Action:當發生InstanceLockedException時發生的操作,該異常發生在當他要去鎖定已經被其他服務宿主鎖定的實例時。有幾個值NoRetry,BasicRetry,AggressiveRetry。

Host Lock Renewal Period:服務宿主在指定時間內必須更新鎖定時間周期。 

下面就簡單說明下如何使用SqlWorkflowInstanceStore來完成工作流的持久化操作,在開始這個例子前需要先建立持久化數據庫。

1.       定義一個ReadLine自定義活動,如下:

public sealed class ReadLine : NativeActivity<string>

    {

        public ReadLine()

        {

        }

 

        public InArgument<string> BookmarkName { get; set; } 

        protected override bool CanInduceIdle

        {

            get

            {

                return true;

            }

        }

 

        protected override void Execute(NativeActivityContext context)

        {

            string name = this.BookmarkName.Get(context);

            if (name == null)

            {

                throw new ArgumentException(string.Format("ReadLine {0}: BookmarkName cannot be null", this.DisplayName), "BookmarkName");

            }

            context.CreateBookmark(name, new BookmarkCallback(OnReadComplete));

        }

 

        void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)

        {

            string input = state as string;

 

            if (input == null)

            {

                throw new ArgumentException(string.Format("ReadLine {0}: ReadLine must be resumed with a non-null string"), "state");

            }

            context.SetValue(base.Result, input);

        }

    }

}

 

2.工作流中我們使用ReadLine等待輸入,設計如下:

clip_image004

3.       宿主程序如下:

namespace CaryPersisten

{

    class Program

    {

        static InstanceStore instanceStore;

        static AutoResetEvent instanceUnloaded = new AutoResetEvent(false);      

        static Guid id;       

 

        static void Main(string[] args)

        {

            instanceStore =new SqlWorkflowInstanceStore("server=.;database=SampleInstanceStore;uid=sa;pwd=123456");

            InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));

            instanceStore.DefaultInstanceOwner = view.InstanceOwner;

 

            WorkflowApplication application = new WorkflowApplication(new Workflow1());

            application.InstanceStore = instanceStore;           

            application.PersistableIdle = (e) =>

            {

                return PersistableIdleAction.Unload;

            };

            application.Unloaded = (e) =>

            {

                instanceUnloaded.Set();

            };

            application.Persist();

            id = application.Id;

            application.Run();

            instanceUnloaded.WaitOne();

 

            string input = Console.ReadLine();

            WorkflowApplication application1 = new WorkflowApplication(new Workflow1());

            application1.InstanceStore = instanceStore;

            application1.Completed = (workflowApplicationCompletedEventArgs) =>

            {

                Console.WriteLine("\nWorkflowApplication has Completed in the {0} state.", workflowApplicationCompletedEventArgs.CompletionState);

            };

            application1.Unloaded = (workflowApplicationEventArgs) =>

            {

                Console.WriteLine("WorkflowApplication has Unloaded\n");

                instanceUnloaded.Set();

            };

            application1.Load(id);

            application1.ResumeBookmark("nabookmark", input);

            instanceUnloaded.WaitOne();           

            Console.ReadLine();

        }

    }

}

 

4.在宿主程序中首先我們創建SqlWorkflowInstanceStore的實例,并將WorkflowApplicaiton的InstanceStore屬性設置為該實例來指定使用的持久化存儲。我們在自定義活動創建了書簽,創建書簽會讓工作流變為idle狀態進行持久化操作,工作流持久化后我們可以看到數據庫的信息如下:

clip_image006

接收到輸入后恢復書簽,并將持久化數據庫中的工作流實例裝載到內存中繼續運行工作流。

  發表評論


新聞頻道:Adobe宣布Flash將停止支持PowerPC G3

推薦鏈接:Windows 7專題發布

網站導航:博客園首頁  個人主頁  新聞  社區  博問  閃存  知識庫


文章來源:http://www.cnblogs.com/carysun/archive/2009/12/09/wf4-sqlworkflowinstancestore.html