ArticleDirectory
- Step 1
- Step 2
- Step 3
- Step 4
- Shows the running result.
- Step 1
- Step 2
- Step 3
- Step 4
- Step 5
Case 1
This article references WF (Windows Workflow Foundation) workflow Learning (I) an ASP. NET state machine verification program. Through this article, we mainly understand the running mechanism of statemachine in WF under Asp.net.
Development Environment: win7 + vs2008 SP1
Step 1
Open vs2008, create a solution wfandaspnet, and add an Asp.net web application to the solution.ProgramWorkflowlibrary, webapplication and state machine workflow library. As shown in.
Complete solution.
ASPnet and sequenceworkflowlibrary are used in the second project, which will be mentioned later.
Step 2
Adding an interface interface1 for workflowlibray has only one method:
String validate (string name );
Step 3
Open the design view of workflow1.cs in workflowlibrary, add two States stateactivity1 and stateactivity2 to it, add eventdriven eventdrivenactivity1 to workflow1initialstate, and add stateinitialation activity worker to the worker state, as shown in.
Select stateactivity2, right-click it, and you will find "set to initial state" and "set to completed state". Here we select "set to completed state ", the icon in the upper-right corner of stateactivity2 changes.
Step 4
Double-click eventdrivenactivity1 To Go To The edit view below.
Add the webserviceinput event to eventdrivenactivity1 and configure the parameter to the activity, as shown in.
The parameters in the attributes are created in the dialog box. Click the small button next to the parameters to bring up the following dialog box. Select the "bind to new member" tab to create the attributes. Shows the configuration method:
Add the webserviceoutput activity to the stateinitialationactivity1 activity and configure properties. As shown in
Double-click webserviceoutputactivity1 to addCode:
Private void webserviceoutputactivity=sendingoutput (Object sender, eventargs e) <br/>{< br/> string name = This. webserviceinputactivity1 _ parameter_name1; <br/> If (name = "xuwei") <br/>{< br/> This. webserviceoutputactivity1 _ returnvalue_1 = "successful"; <br/>}< br/> else <br/>{< br/> This. webserviceoutputactivity1 _ returnvalue_1 = "failed"; <br/>}< br/>}
Procedure
5
Right-click the workflow project workflowlibrary --> select "publish as Web Service". The system automatically generates a web service. Then we reference this web service in the ASP. NET program, add several controls on the ASP. NET page, and then write the background C # code to call the method in this state machine.
Default. aspx. CS code
Namespace webapplication <br/>{< br/> Public partial class _ default: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs E) <br/>{< br/>}< br/> protected void button#click (Object sender, eventargs e) <br/>{< br/> localhost. workflowmediawebservice Ws = new webapplication. localhost. workflowmediawebservice (); <br/> This. label1.text = ws. validate (textbox1.text); <br/>}< br/>}
Front-end code:
<Body> <br/> <Form ID = "form1" runat = "server"> <br/> <p> enter "xuwei" and click "OK, if you enter another key, click OK. </P> <br/> <div> <br/> <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <br/> </div> <br/> <asp: button id = "button1" runat = "server" text = "OK" onclick = "button#click"/> <br/> </div> </P> <p> <Div> <br/> <asp: label id = "label1" runat = "server"> </ASP: label> <br/> </div> <br/> </form> <br/> </body>
Shows the running result.
Case 2
ReferenceWF (Windows Workflow Foundation)Workflow Learning (2) an ASP. NETExample of combining with a sequential Workflow
Step 1
Add the Asp.net web application ASPnet and a sequence workflow library sequenceworkflowlibrary to the wfandaspnet solution.
Step 2
Add a reference to the sequence workflow library sequenceworkflowlibrary and a reference to the workflow assembly for the ASPNET application, as shown in
Step 3
Add the WF registration to Web. config.
<? XML version = "1.0"?> <Br/> <configuration> <br/> <configsections> <br/> <! -- WF registration service --> <br/> <section name = "workflowruntime" type = "system. workflow. runtime. configuration. workflowruntimesection, system. workflow. runtime, version = 3.0.20.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "/> <br/> <! -- End the WF registration service --> <br/> </configsections> <br/> <! -- WF registration service --> <br/> <workflowruntime name = "workflowservicecontainer"> <br/> <services> <br/> <add type = "system. workflow. runtime. hosting. manualworkflowschedulerservice, system. workflow. runtime, version = 3.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "/> <br/> <add type =" system. workflow. runtime. hosting. defaultworkflowcommitworkbatchservice, system. workflow. runtime, version = 3.0.0.0, culture = neutral, Publickeytoken = 31bf3856ad364e35 "/> <br/> </services> <br/> </workflowruntime> <br/> <! -- End the WF registration service --> <br/> <etettings/> <br/> <connectionstrings/> </P> <p> </configuration> <br/>
Step 4
Add a global. asax file and add the following code:
Namespace ASPnet <br/> {<br/> public class Global: system. Web. httpapplication <br/> {<br/> // host WF under Asp.net. <Br/> protected void application_start (Object sender, eventargs e) <br/>{< br/> // read the web. config to create an instance <br/> // <section name = "workflowruntime" type = "system. workflow. runtime. configuration. workflowruntimesection, system. workflow. runtime, version = 3.0.20.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "/> <br/> workflowruntime = new workflowruntime (" workflowruntime "); <br/> Workflowruntime. startruntime (); // start the workflow running time <br/> // specify the current workflow running time <br/> application ["workflowruntime"] = workflowruntime; <br/>}< br/> protected void session_start (Object sender, eventargs e) <br/>{< br/> // obtain the current state of the application. <Br/> workflowruntime = application ["workflowruntime"] As workflowruntime; <br/> // when the workflow is closed <br/> workflowruntime. stopruntime (); <br/>}< br/>}
Step 5
Add a code activity to workflow1.cs in sequenceworkflowlibrary, as shown in
Double-click a code activity to add the code corresponding to the activity.
Namespace sequenceworkflowlibrary <br/>{< br/> Public sealed partial class workflow1: sequentialworkflowactivity <br/>{< br/> Public string name {Get; set ;} <br/> Public String result {Get; Set ;}< br/> Public workflow1 () <br/>{< br/> initializecomponent (); <br/>}< br/> private void codeactivityappsexecutecode (Object sender, eventargs e) <br/>{< br/> result = "hello" + name; <br/>}< br/>
Step 6
Foreground code default. aspx
<Body> <br/> <Form ID = "form1" runat = "server"> <br/> <div> <br/> <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <br/> <asp: button id = "button1" runat = "server" text = "button" onclick = "button#click"/> <br/> <asp: label id = "label1" runat = "server" text = "label"> </ASP: label> <br/> </div> <br/> </form> <br/> </body>
Background code default. aspx. CS
Namespace ASPnet <br/>{< br/> Public partial class _ default: system. web. UI. page <br/> {<br/> string result = string. empty; // global variable <br/> protected void page_load (Object sender, eventargs E) <br/>{< br/>}< br/> // click the button to trigger a workflow event. Click the button to complete all the work. <Br/> protected void button#click (Object sender, eventargs e) <br/>{< br/> startworkflow (); <br/> label1.text = result; <br/>}< br/> private void startworkflow () <br/>{< br/> // obtain the current workflow runtime <br/> workflowruntime = application ["workflowruntime"] As workflowruntime; <br/> // use manualworkflowschedulerservice dynamically to obtain different threads for service. <br/> manualworkflowschedulerservice scheduler = workflowr Untime. getservice (typeof (manualworkflowschedulerservice) as manualworkflowschedulerservice; <br/> // event driver for loading workflowcompleted. This is the action that will be executed after the workflow is completed. The workflow is currently running, so it has not been executed yet. <Br/> workflowruntime. workflowcompleted + = new eventhandler <workflowcompletedeventargs> (workflowruntime_workflowcompleted); <br/> // create a dictionary workflow parameter <br/> dictionary <string, Object> wfpara = new dictionary <string, object> (); <br/> // Add attribute values <br/> wfpara. add ("name", textbox1.text); <br/> // create a workflow instance and pass parameters to the workflow instance. <Br/> workflowinstance = workflowruntime. createworkflow (typeof (sequenceworkflowlibrary. workflow1), wfpara); <br/> // start the workflow instance <br/> workflowinstance. start (); <br/> // schedule a workflow instance and publish the workflow <br/> scheduler. runworkflow (workflowinstance. instanceid); <br/>}< br/> // event-driven workflowcompleted <br/> void workflowruntime_workflowcompleted (Object sender, workflowcompletedeventargs E) <br/>{< br/> If (E. outputparameters. containskey ("result") // If the output parameter contains a parameter whose key is result <br/>{< br/> result = (string) E. outputparameters ["result"]; // assign the value corresponding to the key to the result variable <br/>}< br/>
Shows the program interface: