Trigger call of workflow in three methods in workflow 4.0

Source: Internet
Author: User

1: Use the invoke static method in the workflowinvoker class --> workflowinvoker. Invoke (mywf); // mywf is a custom workflow instance

[This method can execute our workflow in sequence like a function, which is very simple, but data exchange cannot be realized with the workflow during workflow execution]

 

2: Call the run method of the workflowapplication instance --> workflowapplication wfapp = new workflowapplication (mywf); wfapp. Run (); // mywf is the custom workflow instance.

【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.]

 

3: Use the workflowservicehost Class Host to initiate a service, declare a serviceclient class instance on the client, and use this instance to communicate with the previous host service through WCF to trigger workflow calls.

[WorkflowservicehostIs a major workflow host class. You can manage multiple workflow instances at the same time and control the activation of instances.

SupportedWCF, And3.5Compared with more powerful message Association functions. Of course workflowservicehostPersistence is also supported (Persistence) And tracking (Tracking.]

 

The first and second implementation methods are very simple. Here we simply use an example to describe them, as shown below:

--> In a project named myworkflowactivities001 (namespace: myworkflowactivities001), define an activity (write certain content to the file in drive C: Time + current thread ID ):

 Namespace  Myworkflowactivities001 {  Public   Class  Log: codeactivity {  Protected   Override  Void  Execute (codeactivitycontext context ){  Try  {  Using (Streamwriter writer = New Streamwriter ( @"  C: \ log \ mylog.txt  " , True  ) {Writer. writeline (datetime. Now. tostring () + "  -- " + Thread. currentthread. managedthreadid. tostring ());}}  Catch  (Exception ex ){}}}} 

--> Put it into the sequence of A. XAML visualized workflow (defining its namespace and class name myworkflowactivities001.myworkflow) (display name: writelog)

--> Add another project named myworkflowservice001 (namespace: myworkflowservice001) to introduce the myworkflowactivities001 project just now, and then add the followingCode

 Namespace  Myworkflowservice001 {  Class  Program {  Static  Void Main ( String  [] ARGs ){  Try  {Myworkflow mywf = New  Myworkflow (); workflowinvoker. Invoke (mywf );  //  Workflowapplication wfapp = new workflowapplication (mywf );  //  Wfapp. Run ();  Console. Readline ();} Catch  (Exception ex ){}}}} 

* ** The first and second methods of calling are implemented above. ***

 

The third method is as follows: change the content of myworkflowservice001

 Namespace  Myworkflowservice001 {  Class  Program {  Static   Void Main ( String  [] ARGs ){  Try  {Myworkflow mywf =New  Myworkflow (); workflowservicehost host = New Workflowservicehost (mywf, New Uri ( "  Http: // localhost: 18888/hostwfservice  "  ); Servicemetadatabehavior SMB = New  Servicemetadatabehavior (); SMB. httpgetenabled = True ; Host. description. behaviors. Add (SMB); host. open (); console. writeline (  "  OK  "  ); Console. Readline ();}  Catch  (Exception ex ){}}}} 

--> In this way, the URL http: // localhost: 18888/hostwfservice is used to host our services. To be able to call this service in serviceclient mode, we also need to modify writelog. the Visualized workflow model of XAML adds a receiveandsendreply component before calling the Log Activity (This component can receive externally transmitted messages and Make Workflow continue execution ), this component has an attribute: operationname --> This is a method name (I .e., the method name for calling this workflow !!!) Assume that we modify it to startworkflow, then our serviceclient instance can use startworkflow to call workflow. In addition, you can set the passed parameters by setting the content attribute in the receive, and set the content attribute in sendreplytoreceive to set the returned parameter values, as shown in:

 

--> Then create another project consoleapplication1 and add a service reference (http: // localhost: 18888/hostwfservice). [Note: When adding a service reference, set http: // run localhost: 18888/hostwfservice. Add the following code:

 Using  System;  Using  System. Collections. Generic; Using  System. LINQ;  Using  System. text;  Using  Leleapplication1.servicereference1;  Namespace  Leleapplication1 {  Class  Program {  Static   Void Main ( String  [] ARGs ){  Try {Serviceclient Client = New  Serviceclient ();  Bool Result = client. startworlkflow ( "  Haha  "  );}  Catch  (Exception ex ){}}}} 

--> Here, the startworkflow method corresponding to the modified operationname is used to call the corresponding workflow (and a string is passed to workflow, the content attribute value added in the receive can be received --> the parameter name of the corresponding parameter is defined (that is, the parameter of the startworkflow method. The leleapplication1 project needs to reload the corresponding service) and assign to (can be received using a workflow local variable) after the inargument and outargument become much easier)

 

So far, the three methods of starting workflow have been processed. If there is any vulnerability, please correct it.

 

 

 

 

 

 

 

 

 

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.