WF Workflow state machine Workflow development

Source: Internet
Author: User

Overview

A workflow is a modeling of a business process. When designing a workflow, we must first analyze the steps that need to be taken during the business process. Then, we can use WF to create a workflow model to simulate the business processing process.

We know that WF contains two types of workflows: sequential workflow and state machine workflow. A sequential workflow provides a series of organized steps. Generally, the steps are executed one by one. Some steps may need to wait for the occurrence of some events to continue execution. However, sequential workflows are generally used for operations without manual intervention.

A state machine workflow provides a series of States. The workflow starts from the initial state to the end state. Defines the transition between two States. Generally, state machine workflows respond to events, and events change the status.

Business Flow chart of Online Diagnosis

 

Now let's look at the WF status design diagram.

 

The sequence diagram of new workflow is as follows:

 

WF workflow value passing Method

    public sealed partial class CTDiagnosis : StateMachineWorkflowActivity    {        private WorkFlowActivityEntity internalMyActivityData;        public WorkFlowActivityEntity MyActivityDataProperty        {            get { return internalMyActivityData; }            set { internalMyActivityData = value; }        }        public CTDiagnosis()        {            InitializeComponent();        }        private void handleManagerSendBackEvent_Invoked(object sender, ExternalDataEventArgs e)        {            LocalEventArgs localEventArgs = (LocalEventArgs)e;            internalMyActivityData.GotoCondition = localEventArgs.WorkFlowActivityEntityProperty.GotoCondition;        }    }

 

 

 

Get workflow Processor

/// <Summary> /// get the workflow processor /// </summary> public static WorkflowRuntime CurrentWorkflowRuntime {get {if (workflowRuntime = null) {workflowRuntime = new WorkflowRuntime (); // load the communication service ExternalDataExchangeService dataService = new ExternalDataExchangeService (); workflowRuntime. addService (dataService); localService = new LocalService. localService (); dataService. addService (localService); // load the persistence service string connStr = System. configuration. configurationManager. appSettings ["WF. persistenceDB. connectionString "]. toString (); Define WorkflowPersistenceService = new SqlWorkflowPersistenceService (connStr); // bool unloadOnIdle = false; // TimeSpan instanceOwnershipDuration = new TimeSpan (0, 0, 3 ); // TimeSpan loadingInterval = new TimeSpan (0, 0, 1); // WorkflowPersistenceService = // new vertex (connStr, unloadOnIdle, instanceOwnershipDuration, loadingInterval ); // NameValueCollection parms = new NameValueCollection (); // parms. add ("UnloadOnIdle", "true"); // parms. add ("ConnectionString", connStr); // SqlWorkflowPersistenceService WorkflowPersistenceService = // new SqlWorkflowPersistenceService (parms); workflowRuntime. addService (WorkflowPersistenceService); // workflowRuntime of the corresponding process event. workflowIdled + = OnWorkflowIdled; workflowRuntime. workflowStarted + = OnWorkflowStarted; workflowRuntime. workflowTerminated + = OnWorkflowTerminated; workflowRuntime. workflowCompleted + = OnWorkflowCompleted; // load the tracking service // SqlTrackingService sqlTrackingService = new SqlTrackingService (DBAccess. connectionString); // sqlTrackingService. isTransactional = false; // workflowRuntime. addService (sqlTrackingService); workflowRuntime. startRuntime () ;}return workflowRuntime ;}}

 

 

Runtime Service

The WorkflowRuntime class only provides the basic functions for workflow execution. Some important functions we mentioned earlier (such as tracking workflows) can be implemented through the Extended Mechanism-AddService method provided by WorkflowRuntime.

AddService allows us to add available services to the Runtime. These services can be customized for specific fields or built-in services in WF.

Scheduling Services

The scheduling service is used to control the threads required to run the workflow in Runtime.

DefaultWorkflowSchedulerService creates a thread to execute the workflow. because the workflow is separated from the host application thread, the workflow is executed asynchronously and does not block any application thread. you can also configure the maximum number of workflows that can be executed simultaneously.

When the host application can transfer a thread to the workflow Runtime, another scheduling service-ManualWorkflowSchedulerService can be used. server-side applications (such as ASP. NET Web applications and Web Services), it is very useful to transfer threads to Runtime. the common practice of server applications is to create a thread for the Service requested by each client. the significance of the Host application lending the thread "WF Runtime" is that it allows the Runtime to execute the workflow synchronously on the thread of each request, rather than creating two threads for each request.

Like all Windows Worklow services, if the built-in services cannot meet your needs, you can define your own scheduling service.

Transaction Services

The transaction service allows the Runtime to maintain consistency between the state in the workflow and the durable store. the default Transaction Service is an instance of the DefaultWorkflowTransactionService class. activities in a running workflow process share the same process and transaction context with this service.

WF depends on.. NET. transactions namespace to implement the transaction mechanism. the Transactions class provides a lightweight, auto-enlisting, and upgraded transaction. the transaction starts like a local transaction, and then the Runtime can be upgraded to a heavyweight distributed transaction as needed.

Persistence Services

The persistence service saves the workflow status to the persistent storage. sqlWorkflowPersistenceService saves the workflow status to the SQL Server database. persistent services are required for long-running workflows, because we cannot allow the workflow that processes the order form to reside in the memory for 30 days until the customer pays for the order. on the contrary, the Runtime should keep the workflow in the persistent storage state and unload the workflow instance from the memory. in these 30 days, the Runtime can reload the workflow instance and resume running. when the persistence service is enabled, WF Runtime automatically keeps the workflow idle or paused.

SqlWorkflowPersistenceService supports SQL Server 2000 and later (free version of MSDE and Express ). of course, we also need a database architecture to let the persistence service know how to save the workflow status, which will be detailed in the following sections.

Tracking Services

After the scheduling service selects the thread for running the workflow, the tracking service monitors and records the workflow execution information. the tracing service uses the Tracking Profile to notify the Runtime of the type of workflow information it requires. the Tracking service can also open the Tracking Channel to receive events and data. WF contains a SqlTrackingService class that stores tracking data to the SQL Server database. the Tracking Service uses the Transaction Service to ensure that the tracking data of the workflow is consistent with the status of the tracked workflow. the default Runtime does not start the tracking service, but we can add the tracking service programmatically (or use the application configuration file to configure the tracking service ).

 

Get workFlow Template

/// <Summary> /// obtain the workFlow template /// </summary> /// <param name = "workFlowName"> </param> /// <returns> </returns> private Type GetWorkFlowType (string workFlowName) {Type wfType = null; if (workFlowName. equals ("Diagnosis", StringComparison. ordinalIgnoreCase) wfType = typeof (CTDiagnosis); return wfType ;}

 

 

Obtain the ID of a workflow instance.

/// <Summary> /// obtain the ID of the workflow instance /// </summary> /// <param name = "workflowName"> </param> /// <param name = "startParameter"> </param> // <returns> </returns> public Guid NewWorkflow (string workflowName, object startParameter) {Dictionary <string, object> inputParameters = new Dictionary <string, object> (); inputParameters. add ("MyActivityDataProperty", startParameter); // start a new workflow WorkflowInstance work FlowInstance = CurrentWorkflowRuntime. createWorkflow (GetWorkFlowType (workflowName), inputParameters); workflowInstance. start (); // re-load the workflow from the persistent database. This is required. Otherwise, GetLoadedWorkflows cannot obtain the newly added workflow. CurrentWorkflowRuntime. GetWorkflow (workflowInstance. InstanceId); // ReloadWorkflow (); return workflowInstance. InstanceId ;}

 

 

Get the status of workflow

/// <Summary> /// obtain the workflow status /// </summary> /// <param name = "instanceId"> </param> /// <returns> </returns> public string GetInstanceStateByInstanceId (Guid instanceId) {string strStatus = ""; StateMachineWorkflowInstance smwi = new StateMachineWorkflowInstance (WorkflowMgr. currentWorkflowRuntime, instanceId); strStatus = smwi. currentStateName; return strStatus ;}

 

 

Summary

The persistence of the state machine workflow is very important. When workflow is restarted, it cannot be in the correct state.

 

 

References

WF programming Series 4-roaming Workflow: Widows Workflow Runtime and its service http://www.cnblogs.com/xiaoshatian/archive/2007/03/30/693687.html

 

You are welcome to participate in the discussion. If you find it helpful, please clickRecommended. Thank you very much.

Author: spring yang

Source: http://www.cnblogs.com/springyangwc/

The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.