本文转自:http://msdn.microsoft.com/en-us/library/ee264174(v=vs.100).aspx
This topic applies to Windows Workflow Foundation 4 (WF4).
Activities created using .NET Framework 3.0 or .NET Framework 3.5 can be used in a .NET Framework 4 workflow by using the Interop activity. This topic provides an overview of using the Interop activity.
Visual Basic Note: |
---|
The Interop activity does not appear in the workflow designer toolbox unless the workflow's project has its Target Framework setting set to .Net Framework 4.
|
In this topic, a .NET Framework 3.5 activity library is created that contains a DiscountCalculator
activity. The DiscountCalculator
calculates a discount based on a purchase amount and consists of a SequenceActivity that contains a PolicyActivity.
Note: |
---|
The .NET Framework 3.5 activity created in this topic uses a PolicyActivity to implement the logic of the activity. It is not required to use a custom .NET Framework 3.5 activity or the Interop activity in order to use rules in a .NET Framework 4 workflow. For an example of using rules in a .NET Framework 4 workflow without using the Interop activity, see the Policy Activity in .NET Framework 4 sample.
|
Open Visual Studio 2010 and select New and then Project… from the File menu.
Expand the Other Project Types node in the Installed Templates pane and select Visual Studio Solutions.
Select Blank Solution from the Visual Studio Solutions list. Type PolicyInteropDemo in the Name box and click OK.
Right-click PolicyInteropDemo in Solution Explorer and select Add and then New Project….
Tip: |
---|
If the Solution Explorer window is not visible, select Solution Explorer from the View menu.
|
In the Installed Templates list, select Visual C# and then Workflow. Select .NET Framework 3.5 from the .NET Framework version drop-down list, and then select Workflow Activity Library from the Templates list.
Type PolicyActivityLibrary in the Name box and click OK.
Right-click Activity1.cs in Solution Explorer and select Delete. Click OK to confirm.
Right-click PolicyActivityLibrary in Solution Explorer and select Add and then Activity….
Select Activity (with code separation) from the Visual C# Items list. Type DiscountCalculator in the Name box and click OK.
Right-click DiscountCalculator.xoml in Solution Explorer and select View Code.
Add the following three properties to the DiscountCalculator
class.
Right-click DiscountCalculator.xoml in Solution Explorer and select View Designer.
Drag a Policy activity from the Windows Workflow v3.0 section of the Toolbox and drop it in the DiscountCalculator activity.
Tip: |
---|
If the Toolbox window is not visible, select Toolbox from the View menu.
|
Click the newly added Policy activity to select it, if it is not already selected.
Click the RuleSetReference property in the Properties window to select it, and click the ellipsis button to the right of the property.
Tip: |
---|
If the Properties window is not visible, choose Properties Window from the View menu.
|
Select Click New….
Click Add Rule.
Type the following expression into the Condition box.
this.Subtotal >= 50 && this.Subtotal < 100
Type the following expression into the Then Actions box.
this.DiscountPercent = 0.075
Click Add Rule.
Type the following expression into the Condition box.
this.Subtotal >= 100
Type the following expression into the Then Actions box.
this.DiscountPercent = 0.15
Click Add Rule.
Type the following expression into the Condition box.
this.DiscountPercent > 0
Type the following expression into the Then Actions box.
this.Total = this.Subtotal - this.Subtotal * this.DiscountPercent
Type the following expression into the Else Actions box.
this.Total = this.Subtotal
Click OK to close the Rule Set Editor dialog box.
Ensure that the newly-created RuleSet is selected in the Name list, and click OK.
Press CTRL+SHIFT+B to build the solution.
The rules added to the DiscountCalculator
activity in this procedure are shown in the following code example.
Rule1: IF this.Subtotal >= 50 && this.Subtotal < 100 THEN this.DiscountPercent = 0.075 Rule2: IF this. Subtotal >= 100 THEN this.DiscountPercent = 0.15 Rule3: IF this.DiscountPercent > 0 THEN this.Total = this.Subtotal - this.Subtotal * this.DiscountPercent ELSE this.Total = this.Subtotal
When the PolicyActivity executes, these three rules evaluate and modify the Subtotal
, DiscountPercent
, and Total
property values of the DiscountCalculator
activity to calculate the desired discount.
To use the DiscountCalculator
activity inside a .NET Framework 4 workflow, the Interop activity is used. In this section two workflows are created, one using code and one using the workflow designer, which show how to use the Interop activity with the DiscountCalculator
activity. The same host application is used for both workflows.
Right-click PolicyInteropDemo in Solution Explorer and select Add, and then New Project….
Ensure that .NET Framework 4 is selected in the .NET Framework version drop-down list, and select Workflow Console Application from the Visual C# Items list.
Type PolicyInteropHost into the Name box and click OK.
Right-click PolicyInteropHost in Solution Explorer and select Properties.
In the Target framework drop-down list, change the selection from .NET Framework 4 Client Profile to .NET Framework 4. Click Yes to confirm.
Right-click PolicyInteropHost in Solution Explorer and select Add Reference….
Select PolicyActivityLibrary from the Projects tab and click OK.
Right-click PolicyInteropHost in Solution Explorer and select Add Reference….
Select System.Workflow.Activities, System.Workflow.ComponentModel, and then System.Workflow.Runtime from the .NET tab and click OK.
Right-click PolicyInteropHost in Solution Explorer and select Set as StartUp Project.
Press CTRL+SHIFT+B to build the solution.
In this example, a workflow definition is created using code that contains the Interop activity and the DiscountCalculator
activity. This workflow is invoked using WorkflowInvoker and the results of the rule evaluation are written to the console using a WriteLine activity.
Right-click Program.cs in Solution Explorer and select View Code.
Add the following using
statement at the top of the file.
Remove the contents of the Main
method and replace with the following code.
Create a new method in the Program
class called CalculateDiscountUsingCodeWorkflow
that contains the following code.
static void CalculateDiscountUsingCodeWorkflow() { Variable<double> Subtotal = new Variable<double> { Default = 75.99, Name = "Subtotal" }; Variable<double> DiscountPercent = new Variable<double> { Name = "DiscountPercent" }; Variable<double> Total = new Variable<double> { Name = "Total" }; Activity wf = new Sequence { Variables = { Subtotal, DiscountPercent, Total }, Activities = { new Interop { ActivityType = typeof(DiscountCalculator), ActivityProperties = { { "Subtotal", new InArgument<double>(Subtotal) }, { "DiscountPercentOut", new OutArgument<double>(DiscountPercent) }, { "TotalOut", new OutArgument<double>(Total) } } }, new WriteLine { Text = new InArgument<string>(env => string.Format("Subtotal: {0:C}, Discount {1}%, Total {2:C}", Subtotal.Get(env), DiscountPercent.Get(env) * 100, Total.Get(env))) } } }; WorkflowInvoker.Invoke(wf); }
Note: |
---|
The Subtotal , DiscountPercent , and Total properties of the DiscountCalculator activity are surfaced as arguments of the Interop activity, and bound to local workflow variables in the Interop activity’s ActivityProperties collection. Subtotal is added as an In argument because the Subtotal data flows into the Interop activity, and DiscountPercent and Total are added as Out arguments because their data flows out of the Interop activity. Note that the two Out arguments are added with the names DiscountPercentOut and TotalOut to indicate that they represent Out arguments. The DiscountCalculator type is specified as the Interop activity’s ActivityType.
|
Press CTRL+F5 to build and run the application. Substitute different values for the Subtotal
value to test out the different discount levels provided by the DiscountCalculator
activity.
In this example, a workflow is created using the workflow designer. This workflow has the same functionality as the previous example, except than instead of using a WriteLine activity to display the discount, the host application retrieves and displays the discount information when the workflow completes. Also, instead of using local workflow variables to contain the data, arguments are created in the workflow designer and the values are passed in from the host when the workflow is invoked.
Right-click Workflow1.xaml in Solution Explorer and select Delete. Click OK to confirm.
Right-click PolicyInteropHost in Solution Explorer and select Add, New Item….
Expand the Visual C# Items node and select Workflow. Select Activity from the Visual C# Items list.
Type DiscountWorkflow into the Name box and click Add.
Click the Arguments button on the lower left side of the workflow designer to display the Arguments pane.
Click Create Argument.
Type Subtotal into the Name box, select In from the Direction drop-down, select Double from the Argument type drop-down, and then press ENTER to save the argument.
Note: |
---|
If Double is not in the Argument type drop-down list, select Browse for Types …, type System.Double in the Type Name box, and click OK.
|
Click Create Argument.
Type DiscountPercent into the Name box, select Out from the Direction drop-down, select Double from the Argument type drop-down, and then press ENTER to save the argument.
Click Create Argument.
Type Total into the Name box, select Out from the Direction drop-down, select Double from the Argument type drop-down, and then press ENTER to save the argument.
Click the Arguments button on the lower left side of the workflow designer to close the Arguments pane.
Drag a Sequence activity from the Control Flow section of the Toolbox and drop it onto the workflow designer surface.
Drag an Interop activity from the Migration section of the Toolbox and drop it in the Sequence activity.
Click the Interop activity on the Click to browse… label, type DiscountCalculator in the Type Name box, and click OK.
Note: |
---|
When the Interop activity is added to the workflow and the DiscountCalculator type is specified as its ActivityType, the Interop activity exposes three In arguments and three Out arguments that represent the three public properties of the DiscountCalculator activity. The In arguments have the same name as the three public properties, and the three Out arguments have the same names with Out appended to the property name. In the following steps, the workflow arguments created in the previous steps are bound to the Interop activity’s arguments.
|
Type DiscountPercent into the Enter a VB expression box to the right of the DiscountPercentOut property and press TAB.
Type Subtotal into the Enter a VB expression box to the right of the Subtotal property and press TAB.
Type Total into the Enter a VB expression box to the right of the TotalOut property and press TAB.
Right-click Program.cs in Solution Explorer and select View Code.
Add the following using
statement at the top of the file.
Comment out the call to the CalculateDiscountInCode
method in the Main
method and add the following code.
Note: |
---|
If you did not follow the previous procedure and the default Main code is present, replace the contents of Main with the following code.
|
Create a new method in the Program
class called CalculateDiscountUsingDesignerWorkflow
that contains the following code.
static void CalculateDiscountUsingDesignerWorkflow() { double SubtotalValue = 125.99; Dictionary<string, object> wfargs = new Dictionary<string, object> { {"Subtotal", SubtotalValue} }; Activity wf = new DiscountWorkflow(); IDictionary<string, object> outputs = WorkflowInvoker.Invoke(wf, wfargs); Console.WriteLine("Subtotal: {0:C}, Discount {1}%, Total {2:C}", SubtotalValue, (double)outputs["DiscountPercent"] * 100, outputs["Total"]); }
Press CTRL+F5 to build and run the application. To specify a different Subtotal
amount, change the value of SubtotalValue
in the following code.
The WF rules engine provides support for processing rules in a priority-based manner with support for forward chaining. Rules can be evaluated for a single item or for items in a collection. For an overview of rules and information on specific rules functionality, please refer to the following table.
Rules Feature | Documentation |
---|---|
Rules Overview |
Introduction to the Windows Workflow Foundation Rules Engine |
RuleSet |
|
Evaluation of Rules |
|
Rules Chaining |
|
Processing Collections in Rules |
|
Using the PolicyActivity |
Workflows created in .NET Framework 4 do not use all of the rules features provided by WF, such as declarative activity conditions and conditional activities such as the ConditionedActivityGroup and ReplicatorActivity. If required, this functionality is available for workflows created using .NET Framework 3.0 and .NET Framework 3.5. For more information, seeMigrating Workflows.