Insist on learning WF Article Index
Because the rule is notProgramOfCodeYou cannot use the vs Debugger for debugging. If you want to debug a workflow program with rules, you can enable the tracing function of the rule engine. The following is an example. We need to create an ordered workflow project, put a policyactivity in the workflow designer, and set its rulesetreference attribute. We will create the following rule set. See the following table:
Rule |
Conditon |
Thenaction |
Elseaction |
Rulec |
This. d <100 |
This. B = This. B-12 System. Console. writeline ("rulec: Then" + this. B) |
|
Ruleb |
This. B> 50 |
This. c = "preferred" System. Console. writeline ("ruleb: Then" + this. c) |
This. c = "normal" System. Console. writeline ("ruleb: else" + this. c) |
Rulea |
This. A> 10 |
This. B = 60 System. Console. writeline ("rulea: Then" + this. B) |
This. B = 40 System. Console. writeline ("rulea: else" + this. B) |
The rule editor is as follows:
Then we add the app. config file to the project and start the rule tracking function in the configuration file. The Code is as follows:
<? XML Version = " 1.0 " Encoding = " UTF-8 " ?> < Configuration > < System. Diagnostics > < Switches > < Add Name = " System. workflow. Activities. Rules " Value = " All " /> < Add Name = " System. workflow logtofile " Value = " 1 " /> </ Switches > </ System. Diagnostics > </ Configuration >
In this example, the second line adds a log record for the node to enable the file. File NameWorkflowtrace. LogCreate in the Host application directory. The tracelevel attribute value all in the first line indicates that all log information that can be predicted will be recorded. If you want to filter some logs, you can also specify information and verbose, this option is set to equal to "system. workflow logtotracelistener to enable tracelistener logging. After this setting, Windows Workflow Foundation will enumerate each tracelistener created in the Host application and send all logging information to them. Possible values of the value attribute include:
Value |
Description |
All |
Record all received messages |
Off |
Do not record any messages |
Critical |
Record only key messages |
Error |
Record key messages and error messages |
Warning |
Record key, error, and warning messages |
Information |
Record key, error, warning, and prompt messages |
Verbose |
Record key, error, warning, prompt, and detailed message |
The initial values of the response in the workflow are as follows:
Private intA = 12;Private intD = 99;Private intB = 0;Private stringC ="";
The following is the host program code:
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. Threading; Using System. workflow. runtime; Using System. workflow. runtime. Hosting; Namespace Caryruletracing { Class Program { Static void Main ( String [] ARGs ){ Using ( Workflowruntime Workflowruntime = New Workflowruntime ()){ Autoresetevent Waithandle = New Autoresetevent ( False ); Workflowruntime. workflowcompleted + = Delegate ( Object Sender,
Workflowcompletedeventargs E) {waithandle. Set () ;}; workflowruntime. workflowterminated + = Delegate ( Object Sender,
Workflowterminatedeventargs E ){ Console . Writeline (E. Exception. Message); waithandle. Set ();};Workflowinstance Instance = workflowruntime. createworkflow ( Typeof
(Caryruletracing. Workflow1 ); Instance. Start (); waithandle. waitone ();}}}}
Now we have finished the project to run the program. The result is as follows:
Let's open workflowtrace. log to see what is recorded in it. Below are only some records in log. You can open this file to view detailed records:
rule "rulea" condition dependency: " This /A/" side effects of rule "rulea" then: " This /B/" side effects of rule "rulea" else: " This /B/" rule "ruleb" condition dependency: " This /B/" side effects of rule "ruleb" then: " This /C/" side effects of rule "ruleb" else: " thi S /C/" rule" rulec "condition dependency:" This /D/" side effects of rule "rulec" then: " This /B/" rule "rulea" then operation trigger rule "ruleb" rule "rulea" else operation trigger rule "ruleb" rule "rulec" then operation triggers rule "ruleb" execution conditions for calculating rule "rulea. the rule "rulea" condition is calculated as true. the then operation of the calculation rule "rulea. condition of the calculation rule "ruleb. the rule "ruleb" condition is calculated as true. the then operation of the calculation rule "ruleb. condition of the calculation rule "rulec. the rule "rulec" condition is calculated as true. the then operation of the calculation rule "rulec. the side effects of rule "rulec" enable rule "ruleb" to re-calculate. condition of the calculation rule "ruleb. the rule "ruleb" condition is calculated as false. the else operation of the calculation rule "ruleb. |
1. The first part of log records the dependencies of each rule. When a rule is modified, the engine recalculates the rules.
2. From the log, we can see that the rule execution sequence is not set in the Rule editor, but in the same priority, it is executed in alphabetical order. In our example, rulec, ruleb, and rulea are executed in a different order.
3. Through log, we can also confirm the forward link feature of the WF rule engine. In this example, rulec causes the re-calculation of ruleb.
These are only part of the tracking function in WF. We will learn the tracing knowledge in WF later.