Workflowinvoker, workflowapplication and workflowservicehost

Source: Internet
Author: User

Workflow 4.0 is a brand new framework that overwrites the runtime and activity in 3.0/3.5. The biggest difference between runtime in workflow 4.0 and runtime in 3.0/3.5 is that there is no corresponding workflowruntime class.

In 3.0/3.5, We need to initialize workflowruntime and use the createinstance function to create a workflowinstance ). The workflowapplication class in 4.0 corresponds to the workflowinstance class, but it does not need to be instantiated through workflowruntime. It can be instantiated through new.

 

Creating an instance of the workflowruntime class and calling startruntime is all we need to spin up the workflow execution environment. workflowruntime defines methods that allow customization of the execution environment. the class also defines events we can listen for during execution. the runtime will fire an event when workflows finish execution, abort, turn idle, and more.

Once we 've created an instance of the runtime, we can create workflows with the createworkflow method. the createworkflow method returns an object of Type workflowinstance. the workflowinstance class represents an individual workflow. the start method on the workflow instance object will begin in the execution of a workflow. if an exception occurs, the workflow will invoke the terminate method (which leads to the runtime raising a workflowterminated event ). A typical sequence of CILS is shown in the screenshot next.

Here comes a code example below which based on the. NET 3.5:

Class program {


Static autoresetevent waithandle = new autoresetevent (false );
 

Static void main (string [] ARGs) { Workflowruntime = new workflowruntime (); Workflowruntime. workflowcompleted + = New eventhandler <workflowcompletedeventargs> (Workflowruntime_workflowcompleted ); Workflowruntime. workflowterminated + = New eventhandler <workflowterminatedeventargs> (Workflowruntime_workflowterminated ); Workflowinstance instance = workflowruntime. createworkflow (typeof (workflow1 )); Instance. Start (); Waithandle. waitone (); } Static void workflowruntime_workflowterminated (Object sender, workflowterminatedeventargs E) { Console. writeline (E. Exception. Message ); Waithandle. Set (); } Static void workflowruntime_workflowcompleted (Object sender, workflowcompletedeventargs E) { Waithandle. Set (); }}

 

Although workflowapplication corresponds to workflowinstance, their constructor methods are also different. In 3.0/3.5, a system. Type class is passed in, and workflowruntime is used to instantiate the template. In 4.0, we passed in a system. Activities. Activity instance to create a workflow instance.

The code example of how to use workflowapplication class to hosting a WF in. Net 4.0

Activity WF = new sequence { Activities = { New writeline { TEXT = "starting the workflow ." }, New Delay { Duration = timespan. fromseconds (5) }, New writeline { TEXT = "ending the workflow ." } } };
// Create a workflowapplication instance.

Workflowapplication wfapp = new workflowapplication (WF );

//...

// Run the workflow.  

Wfapp. Run ();

 

In addition, 4.0 also provides a fast-track workflowinvoker that calls workflow. You can call the static method invoke to execute a workflow.

Another major feature of workflow 4.0 is the close combination of WCF and workflow. Like 3.5, 4.0 contains a class system. servicemodel. Activities. workflowservicehost used to manage the workflow service. Note that this class exists in system. servicemodel. Activities. dll. It is totally different from workflowservicehost in system. workflowservice. dll (. NET Framework 3.5.

It can be seen that 3 in 4.0 provides different methods to manage/host workflow instances.

Workflowinvoker

You can run a workflow like executing a function.

Advantage: simple

Disadvantage: data exchange with the workflow instance during workflow execution

Code:

Workflowinvoker. Invoke (New workflow1 ());

Workflowapplication

You can control the workflow instance running for a long time and exchange data with the instance during the running process. However, only one workflow instance can be executed.

Advantages:

Persistence is supported. You can control instances and use bookmark to control instances or exchange data.

Example:

Create a wizard UI with the help of WorkflowProgram.

Code:

Workflowapplication instance = new workflowapplication (New workflow1 ());

Instance. Completed = delegate (workflowapplicationcompletedeventargs E)

{

Console. writeline ("workflow completed, id =" + instance. ID );

};

Instance. Run ();

//....

Instance. resumebookmark ("Submit", data );

Workflowservicehost

Workflowservicehost is a major workflow host class. You can manage multiple workflow instances at the same time and control the activation of instances. Supports WCF and has more powerful message Association functions than 3.5. Of course, workflowservicehost also supports persistence and tracking functions.

In my understanding, I think, in most cases,Workflowservicehost manages workflows with an endpoint and receives messages using activities such as receive. This is
Common scenarios of workflowservicehost.

By default, Workflowinvoker Is used to invoke the WF. I changed this Workflowservicehost , The reason is that Workflowinvoker Cannot be configured Persistence . Workflowservicehost Is used to host WF services. The other method of calling (hosting) workflows is Workflowapplication Which-like Workflowservicehost -Can be used to host long running asynch workflows with extensions ( Persistence , Tracking); however, only for workflows that are non services.

So, when your bussiness need to expose the workflow to a WCF Service. You shocould useWorkflowservicehostComplete your task.

// Create a service to handle incoming requestsworkflowservice service = new workflowservice { Name = "libraryreservation ", Body = new processrequest (), Endpoints = { New Endpoint { Servicecontractname = "ilibraryreservation ", Addressuri = new uri ("http: // localhost:" + ADR + "/libraryreservation "), Binding = new basichttpbinding (), }}; // Create a workflowservicehost that listens for incoming messagessystem. servicemodel. Activities. workflowservicehost wsh = new system. servicemodel. Activities. workflowservicehost (service );

Wsh. open ();

 

 

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.