閱讀: 494 評(píng)論: 2 作者: 生魚(yú)片 發(fā)表于 2009-12-09 20:55 原文鏈接

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

我會(huì)系統(tǒng)的介紹下WF4中的持久化服務(wù)。WF4提供了一個(gè)抽象類InstanceStrore,表示邏輯上的工作流實(shí)例的容器,還有一些和持久化相關(guān)的類如LoadWorkflowCommandSaveWorkflowCommand等,具體的實(shí)現(xiàn)我們可以從InstanceStore類繼承來(lái)開(kāi)發(fā)自己的Persistence ProviderWF4已經(jīng)默認(rèn)給我們實(shí)現(xiàn)好了一個(gè)基于SQL的持久化實(shí)現(xiàn),對(duì)應(yīng)于SqlWorkflowInstanceStore類。下面是這兩個(gè)類的繼承關(guān)系:

clip_image002

下面是關(guān)于SqlWorkflowInstanceStore類屬性的一些說(shuō)明:

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

Instance Completion Action:指定當(dāng)工作流實(shí)例完成后的操作(是否刪除持久化存儲(chǔ)中的數(shù)據(jù)),有兩個(gè)值DeleteAll(默認(rèn)),DeleteNothing。

Instance Locked Exception Action:當(dāng)發(fā)生InstanceLockedException時(shí)發(fā)生的操作,該異常發(fā)生在當(dāng)他要去鎖定已經(jīng)被其他服務(wù)宿主鎖定的實(shí)例時(shí)。有幾個(gè)值NoRetry,BasicRetryAggressiveRetry

Host Lock Renewal Period:服務(wù)宿主在指定時(shí)間內(nèi)必須更新鎖定時(shí)間周期。 

下面就簡(jiǎn)單說(shuō)明下如何使用SqlWorkflowInstanceStore來(lái)完成工作流的持久化操作,在開(kāi)始這個(gè)例子前需要先建立持久化數(shù)據(jù)庫(kù)。

1.       定義一個(gè)ReadLine自定義活動(dòng),如下:

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等待輸入,設(shè)計(jì)如下:

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.在宿主程序中首先我們創(chuàng)建SqlWorkflowInstanceStore的實(shí)例,并將WorkflowApplicaiton的InstanceStore屬性設(shè)置為該實(shí)例來(lái)指定使用的持久化存儲(chǔ)。我們?cè)谧远x活動(dòng)創(chuàng)建了書(shū)簽,創(chuàng)建書(shū)簽會(huì)讓工作流變?yōu)?/span>idle狀態(tài)進(jìn)行持久化操作,工作流持久化后我們可以看到數(shù)據(jù)庫(kù)的信息如下:

clip_image006

接收到輸入后恢復(fù)書(shū)簽,并將持久化數(shù)據(jù)庫(kù)中的工作流實(shí)例裝載到內(nèi)存中繼續(xù)運(yùn)行工作流。

  發(fā)表評(píng)論


新聞?lì)l道:Adobe宣布Flash將停止支持PowerPC G3

推薦鏈接:Windows 7專題發(fā)布

網(wǎng)站導(dǎo)航:博客園首頁(yè)  個(gè)人主頁(yè)  新聞  社區(qū)  博問(wèn)  閃存  知識(shí)庫(kù)


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