本文转自: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.

Using the Interop Activity in .NET Framework 4 Workflows

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.

To create the .NET Framework 3.5 activity library project

  1. Open Visual Studio 2010 and select New and then Project… from the File menu.

  2. Expand the Other Project Types node in the Installed Templates pane and select Visual Studio Solutions.

  3. Select Blank Solution from the Visual Studio Solutions list. Type PolicyInteropDemo in the Name box and click OK.

  4. 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.

  5. 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.

  6. Type PolicyActivityLibrary in the Name box and click OK.

  7. Right-click Activity1.cs in Solution Explorer and select Delete. Click OK to confirm.

To create the DiscountCalculator activity

  1. Right-click PolicyActivityLibrary in Solution Explorer and select Add and then Activity….

  2. Select Activity (with code separation) from the Visual C# Items list. Type DiscountCalculator in the Name box and click OK.

  3. Right-click DiscountCalculator.xoml in Solution Explorer and select .

  4. Add the following three properties to the DiscountCalculator class.

    public partial class DiscountCalculator : SequenceActivity
    {
    public double Subtotal { get; set; }
    public double DiscountPercent { get; set; }
    public double Total { get; set; }
    }
  5. Right-click DiscountCalculator.xoml in Solution Explorer and select View Designer.

  6. 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.

To configure the rules

  1. Click the newly added Policy activity to select it, if it is not already selected.

  2. 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.

  3. Select Click New….

  4. Click Add Rule.

  5. Type the following expression into the Condition box.

     
    this.Subtotal >= 50 && this.Subtotal < 100
    
  6. Type the following expression into the Then Actions box.

     
    this.DiscountPercent = 0.075
    
  7. Click Add Rule.

  8. Type the following expression into the Condition box.

     
    this.Subtotal >= 100
    
  9. Type the following expression into the Then Actions box.

     
    this.DiscountPercent = 0.15
    
  10. Click Add Rule.

  11. Type the following expression into the Condition box.

     
    this.DiscountPercent > 0
    
  12. Type the following expression into the Then Actions box.

     
    this.Total = this.Subtotal - this.Subtotal * this.DiscountPercent
    
  13. Type the following expression into the Else Actions box.

     
    this.Total = this.Subtotal
    
  14. Click OK to close the Rule Set Editor dialog box.

  15. Ensure that the newly-created RuleSet is selected in the Name list, and click OK.

  16. 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.

Using the DiscountCalculator Activity with the Interop Activity

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.

To create the host application

  1. Right-click PolicyInteropDemo in Solution Explorer and select Add, and then New Project….

  2. 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.

  3. Type PolicyInteropHost into the Name box and click OK.

  4. Right-click PolicyInteropHost in Solution Explorer and select Properties.

  5. In the Target framework drop-down list, change the selection from .NET Framework 4 Client Profile to .NET Framework 4. Click Yes to confirm.

  6. Right-click PolicyInteropHost in Solution Explorer and select Add Reference….

  7. Select PolicyActivityLibrary from the Projects tab and click OK.

  8. Right-click PolicyInteropHost in Solution Explorer and select Add Reference….

  9. Select System.Workflow.Activities, System.Workflow.ComponentModel, and then System.Workflow.Runtime from the .NET tab and click OK.

  10. Right-click PolicyInteropHost in Solution Explorer and select Set as StartUp Project.

  11. Press CTRL+SHIFT+B to build the solution.

Using the Interop Activity in Code

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.

To use the Interop activity in code

  1. Right-click Program.cs in Solution Explorer and select .

  2. Add the following using statement at the top of the file.

    using PolicyActivityLibrary;
    
  3. Remove the contents of the Main method and replace with the following code.

    static void Main(string[] args)
    {
    CalculateDiscountUsingCodeWorkflow();
    }
  4. 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.

  5. 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.

    Variable<double> Subtotal = new Variable<double>
    {
    Default = 75.99, // Change this value.
    Name = "Subtotal"
    };

Using the Interop Activity in the Workflow Designer

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.

To host the PolicyActivity using a Workflow Designer-created workflow

  1. Right-click Workflow1.xaml in Solution Explorer and select Delete. Click OK to confirm.

  2. Right-click PolicyInteropHost in Solution Explorer and select Add, New Item….

  3. Expand the Visual C# Items node and select Workflow. Select Activity from the Visual C# Items list.

  4. Type DiscountWorkflow into the Name box and click Add.

  5. Click the Arguments button on the lower left side of the workflow designer to display the Arguments pane.

  6. Click Create Argument.

  7. 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.

  8. Click Create Argument.

  9. 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.

  10. Click Create Argument.

  11. 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.

  12. Click the Arguments button on the lower left side of the workflow designer to close the Arguments pane.

  13. Drag a Sequence activity from the Control Flow section of the Toolbox and drop it onto the workflow designer surface.

  14. Drag an Interop activity from the Migration section of the Toolbox and drop it in the Sequence activity.

  15. 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.

  16. Type DiscountPercent into the Enter a VB expression box to the right of the DiscountPercentOut property and press TAB.

  17. Type Subtotal into the Enter a VB expression box to the right of the Subtotal property and press TAB.

  18. Type Total into the Enter a VB expression box to the right of the TotalOut property and press TAB.

  19. Right-click Program.cs in Solution Explorer and select .

  20. Add the following using statement at the top of the file.

    using System.Collections.Generic;
    
  21. 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.

    static void Main(string[] args)
    {
    CalculateDiscountUsingDesignerWorkflow();
    //CalculateDiscountUsingCodeWorkflow();
    }
  22. 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"]);
    }
  23. Press CTRL+F5 to build and run the application. To specify a different Subtotal amount, change the value of SubtotalValue in the following code.

    double SubtotalValue = 125.99; // Change this value.
    

Rules Features Overview

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

Using RuleSets in Workflows and RuleSet

Evaluation of Rules

Rules Evaluation in RuleSets

Rules Chaining

Forward Chaining Control and Forward Chaining of Rules

Processing Collections in Rules

Processing Collections in Rules

Using the PolicyActivity

Using the PolicyActivity Activity and 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.

[转]Using the Interop Activity in a .NET Framework 4 Workflow的更多相关文章

  1. Activity、Task、应用和进程

    http://www.cnblogs.com/franksunny/archive/2012/04/17/2453403.html Activity.Task.应用和进程 为了阅读方便,将文档转成pd ...

  2. VS2012 开发SharePoint 2013 声明式workflow action(activity)之 HelloWorld

    本文讲述VS2012 开发SharePoint 2013 声明式workflow action 之 HelloWorld. 使用VS2012开发客户化的workflow action是SharePoi ...

  3. activity 工作流学习(一)

    一.了解工作流 1.工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实 ...

  4. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q141-Q143)

    Question  141 You are planning an upgrade to a SharePoint 2010 application. You have the following r ...

  5. EF 5 最佳实践白皮书

    Performance Considerations for Entity Framework 5 By David Obando, Eric Dettinger and others Publish ...

  6. Software Engineering: 3. Project planning

    recourse: "Software Engineering", Ian Sommerville Keywords for this chapter: planning sche ...

  7. android实现qq邮箱多个图标效果

    前几天,蛋疼的技术主管非要实现类似装一个qq邮箱,然后能够使用qq邮箱日历的那么一个东西.相当于一个应用生成两个图标,可是不同的是点击不同的图标能够进入不同的应用,例如以下图的效果. 这效果百度了一天 ...

  8. Contoso 大学 - 10 - 高级 EF 应用场景

    原文 Contoso 大学 - 10 - 高级 EF 应用场景 By Tom Dykstra, Tom Dykstra is a Senior Programming Writer on Micros ...

  9. Unity 接MM横屏闪退的原因

    =.=研究了1天接SDK到处都在报错,于是使用logcat查看原因截取到这样的Exception. call to OpenGL ES API withno current context(logge ...

随机推荐

  1. MyEclipse搭建SSH(Struts2+Spring2+Hibernate3)框架项目教程

    对Struts.spring.hibernate大体上了解一遍后,就是针对这个几个框架的整合了. 怎样整合,请看以下: 第一:Struts2的jar和xml配置文件: jar包: commons-fi ...

  2. grep命令使用技巧

    grep如何实现全词查找例如:要查找name这个单词,反馈的查找结果不能包含namespace这样的模式,但是可以包含name()这样的模式,即要查找的单词两端不可以有其他的数字或者字母,但可以有空格 ...

  3. 一个很小的C++写的MVC的例子

    #include<iostream> #include<vector> //get namespace related stuff using std::cin; using ...

  4. pip 安装速度慢解决办法

    https://blog.csdn.net/liujingclan/article/details/50176597 https://blog.csdn.net/rytyy/article/detai ...

  5. JS文件中引用另一个JS文件

    1.生产项目上遇到一个Bug,需要修改JS文件,添加Jquery代码,但是原来的页面没有添加对Jquery文件的引用,无法修改原来的页面(自动生成的HTML) 这就需要在JS文件中添加对Jquery文 ...

  6. iOS开发——高级篇——多线程dispatch_apply

    我们知道遍历数组是一个相对耗时的操作,而同时手机的核是越来越多,所以我们需要充分利用iOS多核的作用. 特别是在遍历操作中还有其他耗时操作.像我们平时直接遍历数组的操作 ,i< ,i++){ / ...

  7. delphi的万能数据库操作

    好多人都抱怨delphi没有提供一个可以把任意数据放入数据库的控件,虽然说用代码实现也不难,但是有控件会更方便,这次我终于还是抽出空来做了这么个控件,以后就可以直接拖放了.它支持把任意数据类型写入数据 ...

  8. Ubuntu 16.04安装JDK/JRE并配置环境变量【转】

    本文转载自:http://www.linuxdiyf.com/linux/30302.html 作为一个Linux新手,在写这篇文章之前,安装了几次jdk,好多次都是环境变量配置错误,导致无法登录系统 ...

  9. 如何编写linux下nand flash驱动-4

    2.       软件方面 如果想要在Linux下编写Nand Flash驱动,那么就先要搞清楚Linux下,关于此部分的整个框架.弄明白,系统是如何管理你的nand flash的,以及,系统都帮你做 ...

  10. 从0开始学习Hadoop(1) 环境准备 Win7环境+VirtureBox+Ubuntu

    虚拟机:VirtureBox 3.18 下载地址: https://www.virtualbox.org/ 操作系统:Ubuntu  版本:ubuntu-15.04-desktop-amd64.iso ...