WF & WCF (5)

Source: Internet
Author: User
Wf4 Persistence: SqlWorkflowInstanceStore2010-06-07 10: 33

 

I will systematically introduce the persistence service in wf4. Wf4 provides an abstract class instancestrore, which indicates the container of the logical workflow instance. There are also some persistence-related classes such as loadworkflowcommand and saveworkflowcommand, we can inherit from the instancestore class to develop our own persistence provider. Wf4 has implemented a SQL-based persistence implementation by default, corresponding to the sqlworkflowinstancestore class. The inheritance relationships of these two classes are as follows:

The following describes the attributes of the sqlworkflowinstancestore class:

Instance encoding option: Specifies the compression algorithm saved to persistent storage. Optional values: gzip and none.

Instance completion action: Specifies whether to delete the data stored in persistent storage after the workflow instance is completed. There are two values: deleteall (default) and deletenothing.

Instance locked exception Action: The operation that occurs when instancelockedexception occurs. This exception occurs when the instance is locked by another service host. There are several values: noretry, basicretry, and aggressiveretry.

Host lock renewal period: the service host must update the lock period within the specified time.

The following describes how to use sqlworkflowinstancestore to perform workflow persistence. Before starting this example, you must create a persistent database.

1. Define a Readline custom activity as follows:

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. In the workflow, we use Readline to wait for input. The design is as follows:

3. The host Program is as follows:

Namespace carypersisten

{

Class Program

{

Static 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. In the Host Program, we first create an instance of sqlworkflowinstancestore and set the instancestore attribute of workflowapplicaiton to this instance to specify the persistent storage used. We have created bookmarks in a custom activity. Creating bookmarks will change the workflow to the idle state for persistence. After the workflow is persisted, we can see the following information about the database:

After receiving the input, restore the bookmarks and load the workflow instance in the persistent database to the memory to continue running the workflow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.