Persistence learning WF (18): using the ipendingwork Interface

Source: Internet
Author: User

[Stick to the top] Stick to learning WF Article Indexes

 

WF regularly checks the persistence point and saves the workflow instance to the persistent storage. In this way, information related to the workflow is stored if an error occurs or the workflow ends abnormally, the next time the workflow instance is loaded, it will start from this point. This is mainly for work-embedded services. The external service of a workflow uses a work batch to retain the status information. These services batch their work items and share the same transactions as the workflow transactions. If the workflow has not committed a transaction, the service can still batch some work items during the durable period. WF provides iworkbatch and ipendingwork to help the service and workflow instance retain status information.

Let's look at a simple example.

1. First define a local serviceIbatchedservices. CS defines a dosomework method to add a pending work item to the workflow,CodeAs follows:

UsingSystem;UsingSystem. workflow. Activities;NamespaceCaryipendingworkdemo{[Externaldataexchange]Public interfaceIbatchedservices{VoidDosomework (StringMessage );}}
 
 
 
Ii. Implement the local service belowBatchedservice. CSThe Code is as follows:
 Using System; Using System. collections; Using System. workflow. runtime;Using System. Transactions; Namespace  Caryipendingworkdemo { Public class  Batchedservice : Ibatchedservices , Ipendingwork { Public void Dosomework ( String Message ){ Workflowenvironment . Workbatch. Add ( This , Message );}Public void Commit ( Transaction Transaction, Icollection Items ){ Foreach ( Object Item In Items ){ Console . Writeline ( "Commiting: {0 }" , Item. tostring ());}} Public void Complete ( Bool Succeeded, Icollection Items ){ If (Succeeded ){ Console . Writeline ( "Complete: Transaction succeeded" );} Else { Console . Writeline ( "Complete: Transaction aborted. Need to rollback" ); Foreach ( Object Item In Items ){Console . Writeline ( "Rolling back: {0 }" , Item. tostring ());}}} Public bool Mustcommit ( Icollection Items ){ Console . Writeline ( "Returning true for mustcommit" ); Return true ;}}}
1. ClassBatchedserviceImplementedIbatchedservices,IpendingworkInterface.
2.The workflowenvironment. workbatch attribute indicates obtaining the current work batch. The add method can add a pending work item to this work batch,
So that the runtime engine can submit all relevant work items in a single transaction. In this example, we only use a simple string for demonstration.
3. The following describes how to implement ipendingwork.
Commit: uses the specified transaction object to submit a list of work items.
Complete: called after the transaction is completed.
Mustcommit: Allow entries in a work batch to assert whether they must be submitted immediately.
 
 
 
 
 
3. Implement a workflow, such
 
 
 
The workflow design is very simple. First drag a transactionscopeactivity, and then add three callexternalmethodactivity
Set their attributes separately, as shown in figure.
 
 
 
 

4. HostProgramAs follows:

Using System; Using System. configuration;   Using System. workflow. runtime; Using System. workflow. Activities; Using System. workflow. runtime. Hosting; Using Bukovics. workflow. Hosting; Using Sharedworkflows; Namespace  Caryipendingworkdemo { Public class  Batchedworktest { Public static void Run (){ Using ( Workflowruntimemanager Manager = New  Workflowruntimemanager ( New  Workflowruntime () {Addservices (Manager. workflowruntime); manager. workflowruntime. startruntime (); Console . Writeline ( "Executing batchedworkworkflow" ); Manager. startworkflow (Typeof (Sharedworkflows. Batchedworkworkflow ), Null ); Manager. waitall (100000 ); Console . Writeline ( "Completed batchedworkworkflow \ n \ r" );}} Private Static void Addservices ( Workflowruntime Instance ){ Sqlworkflowpersistenceservice Persistence = New  Sqlworkflowpersistenceservice (Configurationmanager . Connectionstrings [ "Workflowpersistence" ]. Connectionstring, True , New  Timespan (0, 2, 0 ), New  Timespan (0, 0, 5); instance. addservice (persistence ); Externaldataexchangeservice Exchangeservice = New  Externaldataexchangeservice (); Instance. addservice (exchangeservice); exchangeservice. addservice (New  Batchedservice ());}}}
 
 
 
 
 
5. The program running result is as follows:
 
 
 
 
 
 

When transactionscopeactivity is completed, the transaction is committed. Then establish a persistence point. Use the persistent service class to save the information of the workflow instance.
Ipendingwork is submitted. The descriptions in msdn are as follows:

Operation Sequence when calling Components

1. during initialization, the workflow creates a work batch.

2. The workflow attaches a work batch to a method call to the component. By using the workflowenvironment class, the service can access the work batches in any of its methods.

Operation Order at the submission point

1. The workflow creates a transaction.

2. The workflow cyclically accesses the work batch and collects all the work items of the component (in order) to create the work batch. The workflow passes in transactions and work batches and calls the commit method on the component.

3. The component adds the work in the work batch to the transaction.

4. Repeat steps 1 and 2 for all components of the work item in the work batch.

5. WhenCommitWhen the notification is successful, the workflow submits the corresponding transaction.

6. After the transaction is successfully committed, the workflow cyclically accesses the work batch and collects all work items for each component (as described in step 1 ). The workflow passes in the corresponding transactions and work batches and calls the complete method of each component.

Operation Order of workflow errors

1. The workflow identifies all work items related to the error scope and constructs a work batch.

2. Each unique workflow call sets the completion status of all jobs in the work batch to false.IpendingworkOfCompleteMethod.

3. If the work batch belongs to the subcontext of the transactionscopeactivity activity or compensatingtransactionscopeactivity activity, the workflow waives all work in the work batch.

4. After the error is restored, the reference to all remaining work batches is maintained during the runtime. Later, the job may be submitted at future persistence points.

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.