Mutual invocation of WF workflows and Web Services -- call Workflow workflows through Web Services (develop persistent workflows)

Source: Internet
Author: User

If you have been responsible for developing an enterprise ERP system or an OA system, workflow is no stranger to you. A Workflow is a business rule between workflows and their operation steps.
Abstract, summary, and description. The main problem to be solved in a workflow is that, to achieve a certain business goal, documents, information, or tasks are automatically transmitted between multiple participants based on certain predefined rules using a computer. WF is the key to solving the core issues of enterprises.
Easily implement development according to the business logic, and then publish WF as a Web service, so that the communication between the client and the service and the server will no longer be affected by the development language, and the Web service can easily call WF
To implement business operations.

The following uses a simple order entry system as an example to describe how to publish a Workflow as a Web service.

To implement a persistent workflow, use sqlcmd to create a local database, open the Command Prompt window, and enter

Sqlcom-S localhost \ SQLEXPRRESS-E-Q "create database WorkflowPersistence"

Then open the folder

\ Microsoft. Net \ Framework \ v3.0 \ Windows Workflow Foundation \ SQL \ [Lauguage]

There are 2 script files

SqlPersistenceService_Schema. SQL
, SqlPersistenceService_Logic. SQL

Run this script file on the WorkflowPersistence database, and the database can be created successfully.

Instance Description: when the customer first joins the order, the Web service calls the Start method to create a new Workflow object instance. After that, the AddOrder method can be called multiple times to add a subscription.
The workflow object instance is in a persistent state before the order is submitted. When the server is idle, the relevant data of the Workflow object is stored in
In the WorkflowPersitence database, this can effectively reduce the load on the server cache. After the End method is called to submit the order, the Workflow object will
The data of the Workflow object will be deleted in the database.

Define an Order class first. Do not forget to add the Serializable attribute to the object. Develop an operation class OrderManager for Order, which includes a method AddOrder. When every Order is added, method returns the ID of the newly added Order.

View Code

[Serializable]      

public class Order

{...}

public class OrderManager

{

public int AddOrder(Order order)

{..........}

}

Now, for this instance, we first develop an interface IService_T1. The Start method indicates that the Workflow is started, and the End method indicates that the Workflow is finished.

View Code

namespace Microsoft.IService
{
public interface IService_T1
{
void Start();
int AddOrder(Order order);
void End();
}
}

Is the complete view of this Workflow, we first use webServiceInputActivity1 to start the service

Set the IsActivating attribute of webServiceInputActivity1 to True, which means to activate this activity.
Workflow object instance, set InterfaceType to Microsoft. IService. IService_T1, and
Set MethodName to Start. When the client calls the Start method, the Workflow object instance is activated.

Then set the cycle condition (this. IsRepeated = true) of WhileActivity. this indicates that as long as the value of IsRepeated is True, WhileActivity can continue to run, the Workflow is in the persistent state.

 

Now we have set two event-driven activities for listenActivity1. In the event-driven activity on the left, add webServiceInputActivity2, codeActivity1,
WebServiceOutputActivity1. Set InterfaceType of webServiceInputActivity2
Microsoft. IService. IService_T1, set MethodName to AddOrder, and bind the Parameter order in the AddOrder Method to the parameter _ order in the Workflow object.
(Refer to the complete code), so that you can use webServiceInputActivity2 to mobilize the AddOrder method. Then add the operation code to the codeActivity_ExecuteCode method of codeActivity.

Finally, end the operation through webServiceOutputActivity1, set the InputActivityName attribute to webServiceInputActivity2, and bind the ReturnValue to the variable id.
. In this way, the system can obtain the return value (int) id of the AddOrder method after inserting Order.

Now, you can insert a webServiceInputActivity3 In the event-driven activity on the right and set InterfaceType
Microsoft. IService. IService_T1, set MethodName to End, and then add the handler of the InputReceived event.
WebServiceInputActivity3_InputReceived. This method sets the IsRepeate attribute to false, so that you can call
This activity finally repeats and ends the workflow.

This is the complete code of the Workflow:

View Code

Namespace Microsoft. Workflows
{
Public sealed partial class Workflow: SequentialWorkflowActivity
{
Public Order _ order;
Public int id;
Public bool IsRepeate = true;

Public Workflow2 ()
{
InitializeComponent ();
}
// Execute this operation when the AddOrder method is called, insert order through the orderManager object, and assign the value of orderID to the Workflow parameter id.
Private void codeactivityappsexecutecode (object sender, EventArgs e)
{
OrderManager orderManager = new OrderManager ();

Int orderID = orderManager. AddOrder (order );

This. id = orderID;
}
// When the webServiceInputActivity3 activity is called, set IsRepeate to false to terminate the loop to bundle the workflow object.
Private void webServiceInputActivity3_InputReceived (object sender, EventArgs e)
{
IsRepeate = false;
}
}
}

Right-click the "project" and select "publish this Workflow as Web". The following ASMX file is displayed:

Microsoft. Workflows. Workflow2_WebService.asmx

<% @ WebService class = "Microsoft. Workflows. Workflow2_WebService" %>

Add configuration file

View Code

<? Xml version = "1.0"?>
<Configuration>
<ConfigSections>
<Section name = "WorkflowRuntime" type = "System. Workflow. Runtime. Configuration. WorkflowRuntimeSection, System. Workflow. Runtime, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"/>
</ConfigSections>
<WorkflowRuntime Name = "WorkflowServiceContainer">
<Services>
<Add type = "System. Workflow. Runtime. Hosting. ManualWorkflowSchedulerService, System. Workflow. Runtime, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"/>
<Add type = "System. Workflow. Runtime. Hosting. DefaultWorkflowCommitWorkBatchService, System. Workflow. Runtime, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"/>
<Add type = "System. workflow. runtime. hosting. sqlWorkflowPersistenceService, System. workflow. runtime, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = token "UnloadOnIdle =" true "LoadIntervalSeconds =" 5 "ConnectionString =" Data Source = LESLIE-PC; Initial Catalog = WorkflowPersistence; integrated Security = True "/>

// Here is to add the sqldatabase persistence service for Workflow, because this configuration is required if the Workflow is tested for persistence.
</Services>
</WorkflowRuntime>
<AppSettings/>
<ConnectionStrings/>
<System. web>
<Compilation debug = "true"/>
<Authentication mode = "Windows"/>
<HttpModules>
<Add type = "System. Workflow. Runtime. Hosting. WorkflowWebHostingModule, System. Workflow. Runtime, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35" name = "WorkflowHost"/>
</HttpModules>
</System. web>
</Configuration>

Call the Start method to Start Workflow, and then you can directly call the AddOrder method. You will find that, unlike the previous instance, in the previous example, each instance only
One call is allowed. If the page is not reloaded, an error message is displayed when multiple calls are made. In this example, AddOrder can be called multiple times and run properly, which proves that
The instance object of Workflow has a persistent Workflow. When you do not call End to End the service, this Workflow object can all be running.

Finally, you can call the End method to End the operation. After the operation is completed, call AddOrder. The system will display an error:

System. InvalidOperationException: The workflow with the ID "3a8b9688-fb3f-4a10-bb84-6bf99c30119a" cannot be found in the state persistence storage.

To sum up, with the development of persistent service streams, you can maintain the activity status of the workflow instance, so that you can call each other through multiple Web Services. Using this technology to implement workflow-based applications, you can publish them through the Web service client and maintain the working state.

Web services and WF can be called each other. In these two chapters, we will introduce how to publish a workflow as a Web service, the next chapter describes how to call Web Services in WF through InvokeWebServiceWorkflow.

Mutual invocation of WF workflows and Web Services -- call Workflow workflows (Basic instances) through Web Services)
Mutual invocation of WF workflows and Web Services -- call Workflow workflows through Web Services (develop persistent workflows)
Mutual invocation of WF workflows and Web Services -- using InvokeWebServiceActivity to call Web Services in Workflow workflows
Mutual invocation of WF workflows and Web Services -- mutual invocation of WF and WCF (publishing WF as WCF using ReceiveActivity)

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.