Microsoft released Visual Studio 2010 beta1. After several twists and turns, it finally experienced it. However, the stability of Microsoft beta products is also too poor. Don't complain, now 4.0 of learning materials are still very limited, training kit is better, we recommend you take a look. I will also summarize the learning process. Next I will start to learn WF.
I. What is wf4.0?
Wf4.0 has seen obvious changes in beta1.
Workflow Activity Model: The activity model has changed significantly in wf4.0 beta1. The new workflowelement class replaces the original sequentialworkflowactivity and statemachineworkflowactivity to provide basic abstract behavior. Is the base class of all activities. For example:
Codeactivity and nativeactivity provide basic logic. Our custom activities can be inherited from these classes, or from the workflowelement class.
Built-in standard activities:The wf4.0 beta1 already provides many built-in standard activities, among which the flowchart activity is one of the most interesting new activities. It provides a good compromise between the sequential and statemachine stream control models. Flowchart allows you to use a step-by-step method to implement simple decision making and conversion functions, but it also allows you to return previous activities in the workflow. For many users, the flowchart is usually more intuitive. In addition, some new runtime activities are introduced, which can be used to call the CLR method (methodinvoke) and assign values to workflow variables (assign) and explicitly persist.
Persistence and tracing enhancements:You can use the persist activity to complete workflow state data persistence. And tracking service improvement.
WFEasy extension of the designer:The new WF designer is based on WPF and provides a convenient extension model.
XAML workflow and integration with WCF.
Ii. Create a Workflow
1. First, let's take a look at how to create a workflow in wf4.0 beta1. We create a workflow project of sequence, and we can find that the workflow is fully described in XAML. We drag a writeline activity into the workflow. The function of this activity is to output some information to the console. We only need to set its text attribute. Text is an expression and supports intelligent sensing, for example:
Then we runProgramThe console will output Hello World Cary.
2. The above is the XAML workflow, and we use C #CodeTo build the same workflow, the program is as follows:
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text;Using System. Activities; Using System. Activities. statements; Namespace Workflowconsoleapplication2 { Public class Helloworldcary : Activity { Protected override Workflowelement createbody (){ Return new Sequence () {activities = { New Writeline () {text = "Hello workflow Cary in code" }}};}}}
3. The following is the host program. We can find that there is no workflowruntime in wf4.0 beta1. The Code is as follows:
Class Program { Static void Main ( String [] ARGs) {autoresetevent syncevent = New Autoresetevent ( False ); Workflowinstance myinstance = New Workflowinstance ( New Sequence1 (); myinstance. oncompleted = Delegate (Workflowcompletedeventargs e) {syncevent. Set () ;}; myinstance. onunhandledexception = Delegate (Workflowunhandledexceptioneventargs e) {console. writeline (E. unhandledexception. tostring ()); Return Unhandledexceptionaction. Terminate;}; myinstance. onaborted = Delegate (Workflowabortedeventargs e) {console. writeline (E. Reason); syncevent. Set () ;}; myinstance. Run (); syncevent. waitone ();}}
Iii. workflow Input and Output Parameters
1. In wf4.0beta1The activity uses the following models to store and share data:
Variable: stores data during an activity.
Parameter (arguments): outputs and inputs data in the activity.
Expression: Process Data logic within an activity.
Currently, no dependency attribute binding is used between activities to transmit data. Instead, variables and parameters are used. We add two parameters username and greeting to the workflow. One is the input parameter and the other is the output parameter.
The parameter has an important property direction, which has four values: In, out, in/out, And propery. For example:
Then let's set the return value of the greeting output parameter. We use the assign activity provided by WF to implement it. we drag this activity and set to greeting, set Value to "Hello World" + username, for example:
Click here to insert any activity in the workflow designer. The designer will "drill into" the activity. The navigation in the upper left corner is as follows:
2. The following is the host program. In the Host Program, we use an overload of workflowinstance to pass in the input parameter and get the output parameter of the workflow in oncompleted.
Static void Main ( String [] ARGs) {autoresetevent syncevent = New Autoresetevent ( False ); Console. writeline ("Main () is running on thread {0 }" , Thread. currentthread. managedthreadid); console. Write ( "Enter you name :" ); String Username = console. Readline (); String Greeting = Null ; Dictionary < String , Object > Input = New Dictionary < String , Object > (); Input. Add ( "Username" , Username); workflowinstance myinstance = New Workflowinstance ( New Workflowconsoleapplication2.sequence1 (), input); myinstance. oncompleted = Delegate (Workflowcompletedeventargs e) {console. writeline ( "Oncompleted is running on thread {0 }" , Thread. currentthread. managedthreadid); greeting = E. Outputs [ "Greeting" ]. Tostring (); greeting = outargs. greeting; syncevent. Set () ;}; myinstance. onunhandledexception = Delegate (Workflowunhandledexceptioneventargs e) {console. writeline (E. unhandledexception. tostring ());Return Unhandledexceptionaction. Terminate;}; myinstance. onaborted = Delegate (Workflowabortedeventargs e) {console. writeline (E. reason); syncevent. set () ;}; myinstance. run (); syncevent. waitone (); console. writeline (greeting );}
Iv. Testability
1. in ASP. net mvc recently released by Microsoft, it provides developers with powerful unit testing support, and wf4.0 beta1 also provides enhancements to unit testing. Added the workflowinvoker class to call the workflow and added the Test Capability of WF. The following is the test code for the above program:
[Testmethod]Public voidTesthelloworld () {dictionary <String,Object> Input =NewDictionary <String,Object> (){{"Username","Cary"}}; Idictionary <String,Object> Output; Output = workflowinvoker. Invoke (NewWorkflowconsoleapplication2.sequence1 (), input); Assert. areequal ("Hello workflow, Cary", Output ["Greeting"]);}
Now, this is simple!