9月 12

需要持久化,首先执行微软提供的sql脚本

脚本路径

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\SQL\en

依次执行

SqlWorkflowInstanceStoreSchema.sql

SqlWorkflowInstanceStoreLogic.sql

之后就会看到库中多了很多表和视图

其中 [System.Activities.DurableInstancing].[Instances] 是实例的视图,这个会经常用到

学习来自

https://msdn.microsoft.com/en-us/library/dd489452.aspx

written by ocean

9月 08

本篇主要记录如何让在程序中调用WF

之前两篇的调用方式是阻塞式调用

用的是WorkflowInvoker类来做调用

   Activity workflow1 = new Workflow1();
   IDictionary<string, object> output = WorkflowInvoker.Invoke(workflow1, input);

缺点是会阻塞当前线程,如果是winform的话就会发生界面假死,用在学习中没有问题,如果是工作中就只能使用其他方法

 

WorkflowApplication

工作流宿主,生产环境中使用,它使用的是线程池执行.

官方api

https://msdn.microsoft.com/library/system.activities.workflowapplication(v=vs.110).aspx

从网上拷贝的demo程序

            static void Main(string[] args)
            {
                Activity wf = new WriteLine
                {
                    Text = "WorkflowApplication调用工作流!"
                };
                WorkflowApplication instance1 = new WorkflowApplication(wf);
                instance1.Completed = workflowCompleted;
                instance1.Run();
                Console.WriteLine("Winform线程执行完毕! 线程:" + Thread.CurrentThread.ManagedThreadId);
                Console.ReadKey();
            }
            static void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
            {
                Thread.Sleep(1000);
                Console.WriteLine("流程执行完毕! 线程:" + Thread.CurrentThread.ManagedThreadId);
            }

从运行结果来看,主线程执行完成,工作流线程仍然在执行.

written by ocean

9月 05

首先自定义活动

    public class OceanActivity : CodeActivity
    {
        //定义输入参数
        public InArgument<string> Message { get; set; }
        public InArgument<int> Count { get; set; }
        //定义输出参数
        public OutArgument<int> Result { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            //获取输入参数
            string inputMessge = context.GetValue<string>(this.Message);
            int inputCount = context.GetValue<int>(this.Count);
            //处理逻辑
            var result = 200 + inputCount;
            Console.WriteLine(inputMessge);
            //设置输出参数
            context.SetValue(this.Result, result);
        }
    }

之后设计wf

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WF.CustomerActivity.Workflow1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:local="clr-namespace:WF.CustomerActivity"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="argument1" Type="InArgument(x:Int32)" />
    <x:Property Name="argument2" Type="OutArgument(x:Int32)" />
    <x:Property Name="msg" Type="InArgument(x:String)" />
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <sap2010:WorkflowViewState.IdRef>WF.CustomerActivity.Workflow1_1</sap2010:WorkflowViewState.IdRef>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>WF.CustomerActivity</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_1">
    <local:OceanActivity sap2010:WorkflowViewState.IdRef="OceanActivity_1">
      <local:OceanActivity.Count>
        <InArgument x:TypeArguments="x:Int32">
          <mca:CSharpValue x:TypeArguments="x:Int32">argument1</mca:CSharpValue>
        </InArgument>
      </local:OceanActivity.Count>
      <local:OceanActivity.Message>
        <InArgument x:TypeArguments="x:String">
          <mca:CSharpValue x:TypeArguments="x:String">msg</mca:CSharpValue>
        </InArgument>
      </local:OceanActivity.Message>
      <local:OceanActivity.Result>
        <OutArgument x:TypeArguments="x:Int32">
          <mca:CSharpReference x:TypeArguments="x:Int32">argument2</mca:CSharpReference>
        </OutArgument>
      </local:OceanActivity.Result>
    </local:OceanActivity>
    <sads:DebugSymbol.Symbol>dy1EOlxUZXN0XFdGXFdGLkN1c3RvbWVyQWN0aXZpdHlcV29ya2Zsb3cxLnhhbWwFLQNADgIBAS4FPhsCAQI2CzZMAgELMQsxUQIBBzsLO1kCAQM=</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="OceanActivity_1" sap:VirtualizedContainerService.HintSize="200,22" />
      <sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="222,146">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WF.CustomerActivity.Workflow1_1" sap:VirtualizedContainerService.HintSize="262,226" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>

只需要注意入参和出参即可

最后是调用

            IDictionary<string, object> input = new Dictionary<string, object> { { "argument1", 6 },{"msg","this is a test from ocean."}  };
         
            Activity workflow1 = new Workflow1();
            IDictionary<string, object> output = WorkflowInvoker.Invoke(workflow1, input);
            int result = (int)output["argument2"]; 
             Console.WriteLine("Result=" + result);
              
            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();

 

written by ocean

9月 02

上次研究WF还是两年前啊,后来不了了之.这次又重新启动了.看来最近又要开始学习WF了

 

先来一个个研究下主要的WF控件

InvokeMethod控件

可以用来调用class里的方法,这个非常重要,今天以一个加减乘除做例子

首先class方法如下

    public class OceanTestClass
    { 
        public static int Plus(int a, int b)
        {
            return a + b;
        }
        public static int Minus(int a, int b)
        {
            return a- b;
        }
        public static int Times(int a, int b)
        {
            return a * b;
        }
        public static int Divide(int a, int b)
        {
            return a / b;
        } 
    }

很简单的加减乘除

workflow设计如下

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WF.InvokeMethod.Workflow1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:local="clr-namespace:WF.InvokeMethod"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="a1" Type="InArgument(x:Int32)" />
    <x:Property Name="a2" Type="InArgument(x:Int32)" />
    <x:Property Name="Minus" Type="OutArgument(x:Int32)" />
    <x:Property Name="Plus" Type="OutArgument(x:Int32)" />
    <x:Property Name="Times" Type="OutArgument(x:Int32)" />
    <x:Property Name="Divide" Type="OutArgument(x:Int32)" />
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <sap2010:WorkflowViewState.IdRef>WF.InvokeMethod.Workflow1_1</sap2010:WorkflowViewState.IdRef>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>WF.InvokeMethod</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_1">
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1" Text="Start" />
    <InvokeMethod sap2010:WorkflowViewState.IdRef="InvokeMethod_1" MethodName="Plus" TargetType="local:OceanTestClass">
      <InvokeMethod.Result>
        <OutArgument x:TypeArguments="x:Int32">
          <mca:CSharpReference x:TypeArguments="x:Int32">Plus</mca:CSharpReference>
        </OutArgument>
      </InvokeMethod.Result>
      <InArgument x:TypeArguments="x:Int32">
        <mca:CSharpValue x:TypeArguments="x:Int32">a1</mca:CSharpValue>
      </InArgument>
      <InArgument x:TypeArguments="x:Int32">
        <mca:CSharpValue x:TypeArguments="x:Int32">a2</mca:CSharpValue>
      </InArgument>
    </InvokeMethod>
    <InvokeMethod sap2010:WorkflowViewState.IdRef="InvokeMethod_2" MethodName="Minus" TargetType="local:OceanTestClass">
      <InvokeMethod.Result>
        <OutArgument x:TypeArguments="x:Int32">
          <mca:CSharpReference x:TypeArguments="x:Int32">Minus</mca:CSharpReference>
        </OutArgument>
      </InvokeMethod.Result>
      <InArgument x:TypeArguments="x:Int32">
        <mca:CSharpValue x:TypeArguments="x:Int32">a1</mca:CSharpValue>
      </InArgument>
      <InArgument x:TypeArguments="x:Int32">
        <mca:CSharpValue x:TypeArguments="x:Int32">a2</mca:CSharpValue>
      </InArgument>
    </InvokeMethod>
    <InvokeMethod sap2010:WorkflowViewState.IdRef="InvokeMethod_3" MethodName="Times" TargetType="local:OceanTestClass">
      <InvokeMethod.Result>
        <OutArgument x:TypeArguments="x:Int32">
          <mca:CSharpReference x:TypeArguments="x:Int32">Times</mca:CSharpReference>
        </OutArgument>
      </InvokeMethod.Result>
      <InArgument x:TypeArguments="x:Int32">
        <mca:CSharpValue x:TypeArguments="x:Int32">a1</mca:CSharpValue>
      </InArgument>
      <InArgument x:TypeArguments="x:Int32">
        <mca:CSharpValue x:TypeArguments="x:Int32">a2</mca:CSharpValue>
      </InArgument>
    </InvokeMethod>
    <InvokeMethod sap2010:WorkflowViewState.IdRef="InvokeMethod_4" MethodName="Divide" TargetType="local:OceanTestClass">
      <InvokeMethod.Result>
        <OutArgument x:TypeArguments="x:Int32">
          <mca:CSharpReference x:TypeArguments="x:Int32">Divide</mca:CSharpReference>
        </OutArgument>
      </InvokeMethod.Result>
      <InArgument x:TypeArguments="x:Int32">
        <mca:CSharpValue x:TypeArguments="x:Int32">a1</mca:CSharpValue>
      </InArgument>
      <InArgument x:TypeArguments="x:Int32">
        <mca:CSharpValue x:TypeArguments="x:Int32">a2</mca:CSharpValue>
      </InArgument>
    </InvokeMethod>
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_4" Text="End" />
    <sads:DebugSymbol.Symbol>dylEOlxUZXN0XFdGXFdGLkludm9rZU1ldGhvZFxXb3JrZmxvdzEueGFtbBUwA2gOAgEBMQUxTQIBXDIFPhQCAUY/BUsUAgEwTAVYFAIBGlkFZRQCAQRmBWZLAgECMUMxSgIBXTwJPEgCAVU5CTlIAgFONQs1VAIBR0kJSUgCAT9GCUZIAgE4QgtCVQIBMVYJVkgCASlTCVNIAgEiTwtPVQIBG2MJY0gCARNgCWBIAgEMXAtcVgIBBWZDZkgCAQM=</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="WriteLine_1" sap:VirtualizedContainerService.HintSize="217.333333333333,62.6666666666667" />
      <sap2010:ViewStateData Id="InvokeMethod_1" sap:VirtualizedContainerService.HintSize="217.333333333333,133.333333333333" />
      <sap2010:ViewStateData Id="InvokeMethod_2" sap:VirtualizedContainerService.HintSize="217.333333333333,133.333333333333" />
      <sap2010:ViewStateData Id="InvokeMethod_3" sap:VirtualizedContainerService.HintSize="217.333333333333,133.333333333333" />
      <sap2010:ViewStateData Id="InvokeMethod_4" sap:VirtualizedContainerService.HintSize="217.333333333333,133.333333333333" />
      <sap2010:ViewStateData Id="WriteLine_4" sap:VirtualizedContainerService.HintSize="217.333333333333,62.6666666666667" />
      <sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="239.333333333333,982.666666666667">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WF.InvokeMethod.Workflow1_1" sap:VirtualizedContainerService.HintSize="279.333333333333,1062.66666666667" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>

需要注意在参数处,输入参数 a1,a2 int型

输出参数 Plus,Minus,Times,Divide, int型

 

调用方式

        static void Main(string[] args)
        {

            //输入参数
            IDictionary<string, object> input = new Dictionary<string, object> { { "a1", 6 }, { "a2", 3 }, }; 
            Activity workflow1 = new Workflow1(); 

            //调用返回输出参数
            IDictionary<string, object> output = WorkflowInvoker.Invoke(workflow1, input); 
            int Plus = (int)output["Plus"];
            int Minus = (int)output["Minus"];
            int Times = (int)output["Times"];
            int Divide = (int)output["Divide"]; 

            Console.WriteLine("a1+a2=" + Plus);
            Console.WriteLine("a1-a2=" + Minus);
            Console.WriteLine("a1*a2=" + Times);
            Console.WriteLine("a1/a2=" + Divide);

            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine(); 
        }

 

WF里面以图形界面的方式控制整个流程,简直好玩的不要不要的 

 

 

补充一下,上面的是调用静态方法的

如果是实例方法

在InvokeMethod方法里

TargetType  -> null

TargetObject -> new OceanTestClass()

written by ocean

10月 15

做了几个WF的小练习,为了不至于关掉visual studio就忘记了,特意记录一下

建立的是WorkFlow Console的项目

启动代码

        static void Main(string[] args)
        {  
            WorkflowApplication instance = new WorkflowApplication(new Workflow1());
            //当实例运行完成时调用的方法 
            instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); 
            //打印实例ID 
            System.Console.WriteLine(instance.Id); 
            //启动实例 
            instance.Run(); 
            System.Console.Read(); 
            Console.Read();
        } 
        static void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
        { 
            System.Console.WriteLine("状态:{0}", e.CompletionState.ToString()); 
            System.Console.WriteLine("实例编号:{0}", e.InstanceId); 
        }

控制流

Parallel

并发执行,直到全部执行完再继续往下走

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WFTest.WFConsoleApplication.Workflow1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="argName" Type="InArgument(x:String)" />
    <x:Property Name="argAge" Type="InArgument(x:Int32)" />
    <x:Property Name="argMessage" Type="InArgument(x:String)" />
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <sap2010:WorkflowViewState.IdRef>WFTest.WFConsoleApplication.Workflow1_1</sap2010:WorkflowViewState.IdRef>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>WFTest.WFConsoleApplication</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_6">
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_2" Text="Start" />
    <Parallel sap2010:WorkflowViewState.IdRef="Parallel_1">
      <Sequence sap2010:WorkflowViewState.IdRef="Sequence_3">
        <Sequence.Variables>
          <Variable x:TypeArguments="x:Int32" Name="variable1" />
        </Sequence.Variables>
        <While sap2010:WorkflowViewState.IdRef="While_1">
          <While.Condition>
            <mca:CSharpValue x:TypeArguments="x:Boolean">variable1&lt;18</mca:CSharpValue>
          </While.Condition>
          <Sequence sap2010:WorkflowViewState.IdRef="Sequence_2">
            <Delay Duration="00:00:01" sap2010:WorkflowViewState.IdRef="Delay_1" />
            <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_3">
              <InArgument x:TypeArguments="x:String">
                <mca:CSharpValue x:TypeArguments="x:String">"Left"+variable1</mca:CSharpValue>
              </InArgument>
            </WriteLine>
            <Assign sap2010:WorkflowViewState.IdRef="Assign_1">
              <Assign.To>
                <OutArgument x:TypeArguments="x:Int32">
                  <mca:CSharpReference x:TypeArguments="x:Int32">variable1</mca:CSharpReference>
                </OutArgument>
              </Assign.To>
              <Assign.Value>
                <InArgument x:TypeArguments="x:Int32">
                  <mca:CSharpValue x:TypeArguments="x:Int32">variable1+1</mca:CSharpValue>
                </InArgument>
              </Assign.Value>
            </Assign>
          </Sequence>
        </While>
      </Sequence>
      <While sap2010:WorkflowViewState.IdRef="While_2">
        <While.Variables>
          <Variable x:TypeArguments="x:Int32" Name="variable1" />
        </While.Variables>
        <While.Condition>
          <mca:CSharpValue x:TypeArguments="x:Boolean">variable1&lt;6</mca:CSharpValue>
        </While.Condition>
        <Sequence sap2010:WorkflowViewState.IdRef="Sequence_4">
          <Delay Duration="00:00:02" sap2010:WorkflowViewState.IdRef="Delay_2" />
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_4">
            <InArgument x:TypeArguments="x:String">
              <mca:CSharpValue x:TypeArguments="x:String">"Middle"+variable1</mca:CSharpValue>
            </InArgument>
          </WriteLine>
          <Assign sap2010:WorkflowViewState.IdRef="Assign_2">
            <Assign.To>
              <OutArgument x:TypeArguments="x:Int32">
                <mca:CSharpReference x:TypeArguments="x:Int32">variable1</mca:CSharpReference>
              </OutArgument>
            </Assign.To>
            <Assign.Value>
              <InArgument x:TypeArguments="x:Int32">
                <mca:CSharpValue x:TypeArguments="x:Int32">variable1+1</mca:CSharpValue>
              </InArgument>
            </Assign.Value>
          </Assign>
        </Sequence>
      </While>
      <While sap2010:WorkflowViewState.IdRef="While_3">
        <While.Variables>
          <Variable x:TypeArguments="x:Int32" Name="variable1" />
        </While.Variables>
        <While.Condition>
          <mca:CSharpValue x:TypeArguments="x:Boolean">variable1&lt;4</mca:CSharpValue>
        </While.Condition>
        <Sequence sap2010:WorkflowViewState.IdRef="Sequence_5">
          <Delay Duration="00:00:03" sap2010:WorkflowViewState.IdRef="Delay_3" />
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_5">
            <InArgument x:TypeArguments="x:String">
              <mca:CSharpValue x:TypeArguments="x:String">"right"+variable1</mca:CSharpValue>
            </InArgument>
          </WriteLine>
          <Assign sap2010:WorkflowViewState.IdRef="Assign_3">
            <Assign.To>
              <OutArgument x:TypeArguments="x:Int32">
                <mca:CSharpReference x:TypeArguments="x:Int32">variable1</mca:CSharpReference>
              </OutArgument>
            </Assign.To>
            <Assign.Value>
              <InArgument x:TypeArguments="x:Int32">
                <mca:CSharpValue x:TypeArguments="x:Int32">variable1+1</mca:CSharpValue>
              </InArgument>
            </Assign.Value>
          </Assign>
        </Sequence>
      </While>
    </Parallel>
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_6" Text="over" />
    <sads:DebugSymbol.Symbol>dzlEOlxUZXN0XFdGVGVzdFxXRlRlc3QuV0ZDb25zb2xlQXBwbGljYXRpb25cV29ya2Zsb3cxLnhhbWwlLAOIAQ4CAQEtBS1NAgFULgWFARACAQSGAQWGAUwCAQItQy1KAgFVLwdMEgIBOU0HaA8CAR9pB4QBDwIBBYYBQ4YBSQIBAzMJSxECATpUCWcUAgElUgtSWAIBIHAJgwEUAgELbgtuWAIBBjcLShYCAUA1DTVbAgE7VQtVUgIBN1YLWhcCATFbC2YUAgEmcQtxUgIBHXILdhcCARd3C4IBFAIBDDgNOFQCAVI5DT0ZAgFMPg1JFgIBQVUbVSUCAThYD1hfAgEyYxFjWQIBLF4RXl8CASdxG3ElAgEedA90XgIBGH8Rf1kCARJ6EXpfAgENOB04JwIBUzsRO18CAU1GE0ZbAgFHQRNBYQIBQg==</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="WriteLine_2" sap:VirtualizedContainerService.HintSize="1608,62" />
      <sap2010:ViewStateData Id="Delay_1" sap:VirtualizedContainerService.HintSize="242,22" />
      <sap2010:ViewStateData Id="WriteLine_3" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Assign_1" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Sequence_2" sap:VirtualizedContainerService.HintSize="264,350">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="While_1" sap:VirtualizedContainerService.HintSize="464,510" />
      <sap2010:ViewStateData Id="Sequence_3" sap:VirtualizedContainerService.HintSize="486,634">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="Delay_2" sap:VirtualizedContainerService.HintSize="242,22" />
      <sap2010:ViewStateData Id="WriteLine_4" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Assign_2" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Sequence_4" sap:VirtualizedContainerService.HintSize="264,350">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="While_2" sap:VirtualizedContainerService.HintSize="464,634" />
      <sap2010:ViewStateData Id="Delay_3" sap:VirtualizedContainerService.HintSize="242,22" />
      <sap2010:ViewStateData Id="WriteLine_5" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Assign_3" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Sequence_5" sap:VirtualizedContainerService.HintSize="264,350">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="While_3" sap:VirtualizedContainerService.HintSize="464,634" />
      <sap2010:ViewStateData Id="Parallel_1" sap:VirtualizedContainerService.HintSize="1608,680" />
      <sap2010:ViewStateData Id="WriteLine_6" sap:VirtualizedContainerService.HintSize="1608,62" />
      <sap2010:ViewStateData Id="Sequence_6" sap:VirtualizedContainerService.HintSize="1630,1008">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WFTest.WFConsoleApplication.Workflow1_1" sap:VirtualizedContainerService.HintSize="1670,1088" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>

Pick

分支触发完成,任何一个分支完成,则整个通过,用我还没实际做过WF的脑袋想也能想到这个容器非常重要

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WFTest.WFConsoleApplication.Workflow1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="argName" Type="InArgument(x:String)" />
    <x:Property Name="argAge" Type="InArgument(x:Int32)" />
    <x:Property Name="argMessage" Type="InArgument(x:String)" />
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <sap2010:WorkflowViewState.IdRef>WFTest.WFConsoleApplication.Workflow1_1</sap2010:WorkflowViewState.IdRef>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>WFTest.WFConsoleApplication</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_6">
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_2" Text="Start" />
    <Pick sap2010:WorkflowViewState.IdRef="Pick_1">
      <PickBranch DisplayName="Branch1" sap2010:WorkflowViewState.IdRef="PickBranch_1">
        <PickBranch.Variables>
          <Variable x:TypeArguments="x:Int32" Name="variable1" />
        </PickBranch.Variables>
        <PickBranch.Trigger>
          <While sap2010:WorkflowViewState.IdRef="While_1">
            <While.Condition>
              <mca:CSharpValue x:TypeArguments="x:Boolean">variable1&lt;18</mca:CSharpValue>
            </While.Condition>
            <Sequence sap2010:WorkflowViewState.IdRef="Sequence_2">
              <Delay Duration="00:00:01" sap2010:WorkflowViewState.IdRef="Delay_1" />
              <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_3">
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">"Left"+variable1</mca:CSharpValue>
                </InArgument>
              </WriteLine>
              <Assign sap2010:WorkflowViewState.IdRef="Assign_1">
                <Assign.To>
                  <OutArgument x:TypeArguments="x:Int32">
                    <mca:CSharpReference x:TypeArguments="x:Int32">variable1</mca:CSharpReference>
                  </OutArgument>
                </Assign.To>
                <Assign.Value>
                  <InArgument x:TypeArguments="x:Int32">
                    <mca:CSharpValue x:TypeArguments="x:Int32">variable1+1</mca:CSharpValue>
                  </InArgument>
                </Assign.Value>
              </Assign>
            </Sequence>
          </While>
        </PickBranch.Trigger>
        <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_7" Text="Left Finish" />
      </PickBranch>
      <PickBranch DisplayName="Branch2" sap2010:WorkflowViewState.IdRef="PickBranch_2">
        <PickBranch.Variables>
          <Variable x:TypeArguments="x:Int32" Name="variable1" />
        </PickBranch.Variables>
        <PickBranch.Trigger>
          <While sap2010:WorkflowViewState.IdRef="While_3">
            <While.Condition>
              <mca:CSharpValue x:TypeArguments="x:Boolean">variable1&lt;4</mca:CSharpValue>
            </While.Condition>
            <Sequence sap2010:WorkflowViewState.IdRef="Sequence_5">
              <Delay Duration="00:00:03" sap2010:WorkflowViewState.IdRef="Delay_3" />
              <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_5">
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">"right"+variable1</mca:CSharpValue>
                </InArgument>
              </WriteLine>
              <Assign sap2010:WorkflowViewState.IdRef="Assign_3">
                <Assign.To>
                  <OutArgument x:TypeArguments="x:Int32">
                    <mca:CSharpReference x:TypeArguments="x:Int32">variable1</mca:CSharpReference>
                  </OutArgument>
                </Assign.To>
                <Assign.Value>
                  <InArgument x:TypeArguments="x:Int32">
                    <mca:CSharpValue x:TypeArguments="x:Int32">variable1+1</mca:CSharpValue>
                  </InArgument>
                </Assign.Value>
              </Assign>
            </Sequence>
          </While>
        </PickBranch.Trigger>
        <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_8" Text="Right Finish" />
      </PickBranch>
    </Pick>
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_6" Text="over" />
    <sads:DebugSymbol.Symbol>dzlEOlxUZXN0XFdGVGVzdFxXRlRlc3QuV0ZDb25zb2xlQXBwbGljYXRpb25cV29ya2Zsb3cxLnhhbWwgLAN0DgIBAS0FLU0CAT8uBXEMAgEEcgVyTAIBAi1DLUoCAUAvB08UAgEiUAdwFAIBBXJDckkCAQM0C0wTAgElTglOVwIBI1ULbRMCAQhvCW9YAgEGOA1LGAIBKzYPNl0CASZOR05UAgEkWQ1sGAIBDlcPV1wCAQlvR29VAgEHOQ85VgIBPToPPhsCATc/D0oYAgEsWg9aVgIBIFsPXxsCARpgD2sYAgEPOR85KQIBPjwTPGECAThHFUddAgEyQhVCYwIBLVofWikCASFdE11iAgEbaBVoXQIBFWMVY2MCARA=</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="WriteLine_2" sap:VirtualizedContainerService.HintSize="1142,62" />
      <sap2010:ViewStateData Id="Delay_1" sap:VirtualizedContainerService.HintSize="242,22" />
      <sap2010:ViewStateData Id="WriteLine_3" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Assign_1" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Sequence_2" sap:VirtualizedContainerService.HintSize="264,350">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="While_1" sap:VirtualizedContainerService.HintSize="464,510" />
      <sap2010:ViewStateData Id="WriteLine_7" sap:VirtualizedContainerService.HintSize="211,62" />
      <sap2010:ViewStateData Id="PickBranch_1" sap:VirtualizedContainerService.HintSize="494,770" />
      <sap2010:ViewStateData Id="Delay_3" sap:VirtualizedContainerService.HintSize="242,22" />
      <sap2010:ViewStateData Id="WriteLine_5" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Assign_3" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Sequence_5" sap:VirtualizedContainerService.HintSize="264,350">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="While_3" sap:VirtualizedContainerService.HintSize="464,510" />
      <sap2010:ViewStateData Id="WriteLine_8" sap:VirtualizedContainerService.HintSize="211,62" />
      <sap2010:ViewStateData Id="PickBranch_2" sap:VirtualizedContainerService.HintSize="494,770" />
      <sap2010:ViewStateData Id="Pick_1" sap:VirtualizedContainerService.HintSize="1142,816" />
      <sap2010:ViewStateData Id="WriteLine_6" sap:VirtualizedContainerService.HintSize="1142,62" />
      <sap2010:ViewStateData Id="Sequence_6" sap:VirtualizedContainerService.HintSize="1164,1144">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WFTest.WFConsoleApplication.Workflow1_1" sap:VirtualizedContainerService.HintSize="1204,1224" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>

ParallelForEach

同paralle,只不过都是相同任务的List作为参数

集合操作

AddToCollection

ClearCollection

ExistInCollection

RemoveFromCollection

这几个都比较简单,顾名思义,看名字就知道是干什么的

代码就简单写一下啦

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WFTest.WFConsoleApplication.Workflow1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="argName" Type="InArgument(x:String)" />
    <x:Property Name="argAge" Type="InArgument(x:Int32)" />
    <x:Property Name="argMessage" Type="InArgument(x:String)" />
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <sap2010:WorkflowViewState.IdRef>WFTest.WFConsoleApplication.Workflow1_1</sap2010:WorkflowViewState.IdRef>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>WFTest.WFConsoleApplication</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_7">
    <Sequence.Variables>
      <Variable x:TypeArguments="scg:List(x:Int32)" Name="variable1">
        <Variable.Default>
          <mca:CSharpValue x:TypeArguments="scg:List(x:Int32)">new List&lt;int&gt;()</mca:CSharpValue>
        </Variable.Default>
      </Variable>
    </Sequence.Variables>
    <AddToCollection x:TypeArguments="x:Int32" sap2010:WorkflowViewState.IdRef="AddToCollection`1_1" Item="1">
      <InArgument x:TypeArguments="scg:ICollection(x:Int32)">
        <mca:CSharpValue x:TypeArguments="scg:ICollection(x:Int32)">variable1</mca:CSharpValue>
      </InArgument>
    </AddToCollection>
    <AddToCollection x:TypeArguments="x:Int32" sap2010:WorkflowViewState.IdRef="AddToCollection`1_2" Item="2">
      <InArgument x:TypeArguments="scg:ICollection(x:Int32)">
        <mca:CSharpValue x:TypeArguments="scg:ICollection(x:Int32)">variable1</mca:CSharpValue>
      </InArgument>
    </AddToCollection>
    <RemoveFromCollection x:TypeArguments="x:Int32" sap2010:WorkflowViewState.IdRef="RemoveFromCollection`1_1" Item="2">
      <InArgument x:TypeArguments="scg:ICollection(x:Int32)">
        <mca:CSharpValue x:TypeArguments="scg:ICollection(x:Int32)">variable1</mca:CSharpValue>
      </InArgument>
    </RemoveFromCollection>
    <AddToCollection x:TypeArguments="x:Int32" sap2010:WorkflowViewState.IdRef="AddToCollection`1_3" Item="3">
      <InArgument x:TypeArguments="scg:ICollection(x:Int32)">
        <mca:CSharpValue x:TypeArguments="scg:ICollection(x:Int32)">variable1</mca:CSharpValue>
      </InArgument>
    </AddToCollection>
    <ForEach x:TypeArguments="x:Int32" sap2010:WorkflowViewState.IdRef="ForEach`1_1">
      <ForEach.Values>
        <InArgument x:TypeArguments="scg:IEnumerable(x:Int32)">
          <mca:CSharpValue x:TypeArguments="scg:IEnumerable(x:Int32)">variable1</mca:CSharpValue>
        </InArgument>
      </ForEach.Values>
      <ActivityAction x:TypeArguments="x:Int32">
        <ActivityAction.Argument>
          <DelegateInArgument x:TypeArguments="x:Int32" Name="item" />
        </ActivityAction.Argument>
        <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1">
          <InArgument x:TypeArguments="x:String">
            <mca:CSharpValue x:TypeArguments="x:String">item.ToString()</mca:CSharpValue>
          </InArgument>
        </WriteLine>
      </ActivityAction>
    </ForEach>
    <sads:DebugSymbol.Symbol>dzlEOlxUZXN0XFdGVGVzdFxXRlRlc3QuV0ZDb25zb2xlQXBwbGljYXRpb25cV29ya2Zsb3cxLnhhbWwSLANaDgIBATALMGcCAQI0BTgXAgEpOQU9FwIBIj4FQhwCARtDBUcXAgEUSAVYDwIBBzYJNmACASs0azRuAgEqOwk7YAIBJDlrOW4CASNACUBgAgEdPnU+eAIBHEUJRWACARZDa0NuAgEVSwtLYgIBD1IJVhUCAQhUDVRaAgEJ</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="AddToCollection`1_1" sap:VirtualizedContainerService.HintSize="287,22" />
      <sap2010:ViewStateData Id="AddToCollection`1_2" sap:VirtualizedContainerService.HintSize="287,22" />
      <sap2010:ViewStateData Id="RemoveFromCollection`1_1" sap:VirtualizedContainerService.HintSize="287,22" />
      <sap2010:ViewStateData Id="AddToCollection`1_3" sap:VirtualizedContainerService.HintSize="287,22" />
      <sap2010:ViewStateData Id="WriteLine_1" sap:VirtualizedContainerService.HintSize="211,62" />
      <sap2010:ViewStateData Id="ForEach`1_1" sap:VirtualizedContainerService.HintSize="287,212" />
      <sap2010:ViewStateData Id="Sequence_7" sap:VirtualizedContainerService.HintSize="309,584">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WFTest.WFConsoleApplication.Workflow1_1" sap:VirtualizedContainerService.HintSize="349,664" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>

Parallel中使用 CancellationScope

在WF4中,可以使用[Parallel] 并行执行多条分支,当[Parallel]中的所有分支都执行完成后,[Parallel]结束.

可以使用[Pick]实现多条等待分支的单线执行,当一条分支被触发后,其它分支就不会被触发了,当触发的分支完成后,[Pick]结束

但有时我们会的这样一种需求,我们需要并行执行多条分支,当并行分支中的一条或几条分支满足指定条件时,其它正在执行的分支就不执了.同时,为了保证数据的完整性,我们需要在那些可能要取消的分支中余留一组代码.这组代码用于在该分支被取消后做一些收尾工做

可以在[Parallel]容器中使用[CancellationScope],当[Parallel]的[CompletionCondition]属性为[True]时,[Parallel]容器会在其内部[CancellationScope]容器执行完成后,结束其它正在执行的并行分支.如果其它正在执行的并行分支是[CancellationScope],则会调用该[CancellationScope]的[CancelHandler]

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WFTest.WFConsoleApplication.Workflow1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="argName" Type="InArgument(x:String)" />
    <x:Property Name="argAge" Type="InArgument(x:Int32)" />
    <x:Property Name="argMessage" Type="InArgument(x:String)" />
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <sap2010:WorkflowViewState.IdRef>WFTest.WFConsoleApplication.Workflow1_1</sap2010:WorkflowViewState.IdRef>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>WFTest.WFConsoleApplication</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_10">
    <Sequence.Variables>
      <Variable x:TypeArguments="x:Boolean" Default="False" Name="variable1" />
    </Sequence.Variables>
    <Parallel sap2010:WorkflowViewState.IdRef="Parallel_1">
      <Parallel.CompletionCondition>
        <mca:CSharpValue x:TypeArguments="x:Boolean">variable1</mca:CSharpValue>
      </Parallel.CompletionCondition>
      <CancellationScope sap2010:WorkflowViewState.IdRef="CancellationScope_1">
        <Sequence sap2010:WorkflowViewState.IdRef="Sequence_8">
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_2" Text="执行A第一步" />
          <Delay Duration="00:00:03" sap2010:WorkflowViewState.IdRef="Delay_1" />
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_3" Text="执行A第二步" />
          <Assign sap2010:WorkflowViewState.IdRef="Assign_1">
            <Assign.To>
              <OutArgument x:TypeArguments="x:Boolean">
                <mca:CSharpReference x:TypeArguments="x:Boolean">variable1</mca:CSharpReference>
              </OutArgument>
            </Assign.To>
            <Assign.Value>
              <InArgument x:TypeArguments="x:Boolean">True</InArgument>
            </Assign.Value>
          </Assign>
        </Sequence>
        <CancellationScope.CancellationHandler>
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_4" Text="A执行完了,B不需要执行,So清除B已执行的步骤" />
        </CancellationScope.CancellationHandler>
      </CancellationScope>
      <CancellationScope sap2010:WorkflowViewState.IdRef="CancellationScope_2">
        <Sequence sap2010:WorkflowViewState.IdRef="Sequence_9">
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_5" Text="执行B第一步" />
          <Delay Duration="00:00:05" sap2010:WorkflowViewState.IdRef="Delay_2" />
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_6" Text="执行B第二步" />
          <Assign sap2010:WorkflowViewState.IdRef="Assign_2">
            <Assign.To>
              <OutArgument x:TypeArguments="x:Boolean">
                <mca:CSharpReference x:TypeArguments="x:Boolean">variable1</mca:CSharpReference>
              </OutArgument>
            </Assign.To>
            <Assign.Value>
              <InArgument x:TypeArguments="x:Boolean">True</InArgument>
            </Assign.Value>
          </Assign>
        </Sequence>
        <CancellationScope.CancellationHandler>
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_7" Text="B执行完了,A不需要执行,So清除B已执行的步骤" />
        </CancellationScope.CancellationHandler>
      </CancellationScope>
    </Parallel>
    <sads:DebugSymbol.Symbol>dzlEOlxUZXN0XFdGVGVzdFxXRlRlc3QuV0ZDb25zb2xlQXBwbGljYXRpb25cV29ya2Zsb3cxLnhhbWweLANeDgIBAS41LjwCAQIwBVwQAgEDNAdHGwIBGkgHWxsCAQkyCTJRAgEENQlDFAIBHUULRWYCARtJCVcUAgEMWQtZZgIBCjYLNlQCASk3CzdSAgEnOAs4VAIBJTkLQhQCAR5FSUVjAgEcSgtKVAIBGEsLS1ICARZMC0xUAgEUTQtWFAIBDVlJWWMCAQs2STZRAgEqNxs3JQIBKDhJOFECASZAN0A7AgEkPBE8YQIBH0pJSlECARlLG0slAgEXTElMUQIBFVQ3VDsCARNQEVBhAgEO</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="WriteLine_2" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Delay_1" sap:VirtualizedContainerService.HintSize="242,22" />
      <sap2010:ViewStateData Id="WriteLine_3" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Assign_1" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Sequence_8" sap:VirtualizedContainerService.HintSize="264,452">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WriteLine_4" sap:VirtualizedContainerService.HintSize="211,62" />
      <sap2010:ViewStateData Id="CancellationScope_1" sap:VirtualizedContainerService.HintSize="300,721" />
      <sap2010:ViewStateData Id="WriteLine_5" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Delay_2" sap:VirtualizedContainerService.HintSize="242,22" />
      <sap2010:ViewStateData Id="WriteLine_6" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Assign_2" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Sequence_9" sap:VirtualizedContainerService.HintSize="264,452">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WriteLine_7" sap:VirtualizedContainerService.HintSize="211,62" />
      <sap2010:ViewStateData Id="CancellationScope_2" sap:VirtualizedContainerService.HintSize="300,721" />
      <sap2010:ViewStateData Id="Parallel_1" sap:VirtualizedContainerService.HintSize="754,767" />
      <sap2010:ViewStateData Id="Sequence_10" sap:VirtualizedContainerService.HintSize="776,891">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WFTest.WFConsoleApplication.Workflow1_1" sap:VirtualizedContainerService.HintSize="816,971" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>

执行结果

ac056131-fa2c-4bb1-98f7-e59dd55075dc
执行A第一步
执行B第一步
执行A第二步
B执行完了,A不需要执行,So清除B已执行的步骤
状态:Closed
实例编号:ac056131-fa2c-4bb1-98f7-e59dd55075dc

InvokeMethod 调用方法

直接调用C#的方法,分为调用静态方法和调用示例方法两种

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WFTest.WFConsoleApplication.Workflow1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:local="clr-namespace:WFTest.WFConsoleApplication"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="argName" Type="InArgument(x:String)" />
    <x:Property Name="argAge" Type="InArgument(x:Int32)" />
    <x:Property Name="argMessage" Type="InArgument(x:String)" />
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <sap2010:WorkflowViewState.IdRef>WFTest.WFConsoleApplication.Workflow1_1</sap2010:WorkflowViewState.IdRef>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>WFTest.WFConsoleApplication</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_11">
    <InvokeMethod sap2010:WorkflowViewState.IdRef="InvokeMethod_1" MethodName="SayHelloStatic" TargetType="local:OceanTest">
      <InArgument x:TypeArguments="x:String">ocean</InArgument>
    </InvokeMethod>
    <InvokeMethod sap2010:WorkflowViewState.IdRef="InvokeMethod_2" MethodName="SayHelloInstance">
      <InvokeMethod.TargetObject>
        <InArgument x:TypeArguments="local:OceanTest">
          <mca:CSharpValue x:TypeArguments="local:OceanTest">new OceanTest()</mca:CSharpValue>
        </InArgument>
      </InvokeMethod.TargetObject>
      <InArgument x:TypeArguments="x:String">Ocean</InArgument>
    </InvokeMethod>
    <sads:DebugSymbol.Symbol>dzlEOlxUZXN0XFdGVGVzdFxXRlRlc3QuV0ZDb25zb2xlQXBwbGljYXRpb25cV29ya2Zsb3cxLnhhbWwGLQM6DgIBAS4FMBQCAQgxBTgUAgECLy4vMwIBCTQLNF8CAQQ3LjczAgED</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="InvokeMethod_1" sap:VirtualizedContainerService.HintSize="218,130" />
      <sap2010:ViewStateData Id="InvokeMethod_2" sap:VirtualizedContainerService.HintSize="218,130" />
      <sap2010:ViewStateData Id="Sequence_11" sap:VirtualizedContainerService.HintSize="240,424">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WFTest.WFConsoleApplication.Workflow1_1" sap:VirtualizedContainerService.HintSize="280,504" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>
    public class OceanTest
    { 
        public static void SayHelloStatic(string name)
        {
            string msg= "Hello " + name + ". I am from blog.wx6.org Static";
            Console.WriteLine(msg);
        } 
        public   void SayHelloInstance(string name)
        {
            string msg = "Hello " + name + ". I am from blog.wx6.org Instance";
            Console.WriteLine(msg);
        }
    }

written by ocean

10月 19

最近公司项目要做Workflow, 查了一圈下来觉得还是用Microsoft的最好

陆陆续续学了一些东西,最大的疑问就是这些东西怎么实践到具体的项目

找到一个例子 Workflow结合ASP.NET的, 写的挺好,记录一下

基于Visual Studio 2010

Microsoft Windows Workflow Foundation (WWF) 4.5

首先画业务流程,如下(XAML记录)

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WFTest.ActivityLibrary.Greeting"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="Result" Type="OutArgument(x:String)" />
    <x:Property Name="ArgUserName" Type="InArgument(x:String)" />
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <sap2010:WorkflowViewState.IdRef>WFTest.ActivityLibrary.Greeting_1</sap2010:WorkflowViewState.IdRef>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>PresentationFramework</AssemblyReference>
      <AssemblyReference>PresentationCore</AssemblyReference>
      <AssemblyReference>System.Activities.Presentation</AssemblyReference>
      <AssemblyReference>WindowsBase</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>WFTest.ActivityLibrary</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_1">
    <Assign sap2010:WorkflowViewState.IdRef="Assign_1">
      <Assign.To>
        <OutArgument x:TypeArguments="x:String">
          <mca:CSharpReference x:TypeArguments="x:String">Result</mca:CSharpReference>
        </OutArgument>
      </Assign.To>
      <Assign.Value>
        <InArgument x:TypeArguments="x:String">
          <mca:CSharpValue x:TypeArguments="x:String">"Hello " + ArgUserName</mca:CSharpValue>
        </InArgument>
      </Assign.Value>
    </Assign>
    <sads:DebugSymbol.Symbol>dzNEOlxUZXN0XFdGVGVzdFxXRlRlc3QuQWN0aXZpdHlMaWJyYXJ5XEdyZWV0aW5nLnhhbWwELwM9DgIBATAFOw4CAQI4CzhfAgEGMwszVwIBAw==</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="Assign_1" sap:VirtualizedContainerService.HintSize="242,62" />
      <sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="264,186">
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </sap2010:ViewStateData>
      <sap2010:ViewStateData Id="WFTest.ActivityLibrary.Greeting_1" sap:VirtualizedContainerService.HintSize="304,266" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>

ASP.NET调用

如下

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Workflow.Web.Default" %> 
   
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
   
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:Label Text="Your name: " runat="server" /> 
        <asp:TextBox ID="TextBoxName" runat="server" /> 
        <asp:Button ID="btnClick" Text="Test Click" runat="server"
            onclick="btnClick_Click" /> 
        <br /> 
        <asp:Label ID="LabelGreeting" Text="" runat="server" /> 
    </div> 
    </form> 
</body> 
</html>
using System; 
using System.Activities; 
using System.Collections.Generic; 
using WorkflowLibrary; 
   
namespace Workflow.Web 
{ 
    public partial class Default : System.Web.UI.Page 
    {  
        protected void btnClick_Click(object sender, EventArgs e) 
        { 
            string username = TextBoxName.Text; 
   
            Greeting greeting = new Greeting { ArgUserName = username }; 
   
            IDictionary<string, object> results = WorkflowInvoker.Invoke(greeting); 
            LabelGreeting.Text = results["Result"].ToString(); 
        } 
    } 
}

 

学习地址:http://social.technet.microsoft.com/wiki/contents/articles/5284.getting-started-windows-workflow-foundation-and-asp-net.aspx

written by ocean

10月 15

主要用到了 SqlWorkflowInstanceStore

SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore(Const.ConnectionString);  
InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));  
instanceStore.DefaultInstanceOwner = view.InstanceOwner;

触发持久化数据库

application.Unload();  
    
或者              
    
            application.PersistableIdle = (e) =>  
            {  
                return PersistableIdleAction.Unload;  
            };

 全部代码

 

流程

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WFTest.WorkflowConsole.Activity1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:local="clr-namespace:WFTest.WorkflowConsole"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
  <x:Members> 
    <x:Property Name="argument1" Type="OutArgument(x:String)" /> 
  </x:Members> 
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor> 
  <sap2010:WorkflowViewState.IdRef>WFTest.WorkflowConsole.Activity1_1</sap2010:WorkflowViewState.IdRef> 
  <TextExpression.NamespacesForImplementation> 
    <sco:Collection x:TypeArguments="x:String"> 
      <x:String>System</x:String> 
      <x:String>System.Collections.Generic</x:String> 
      <x:String>System.Data</x:String> 
      <x:String>System.Linq</x:String> 
      <x:String>System.Text</x:String> 
    </sco:Collection> 
  </TextExpression.NamespacesForImplementation> 
  <TextExpression.ReferencesForImplementation> 
    <sco:Collection x:TypeArguments="AssemblyReference"> 
      <AssemblyReference>Microsoft.CSharp</AssemblyReference> 
      <AssemblyReference>System</AssemblyReference> 
      <AssemblyReference>System.Activities</AssemblyReference> 
      <AssemblyReference>System.Activities.DurableInstancing</AssemblyReference> 
      <AssemblyReference>System.Core</AssemblyReference> 
      <AssemblyReference>System.Data</AssemblyReference> 
      <AssemblyReference>System.Runtime.DurableInstancing</AssemblyReference> 
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference> 
      <AssemblyReference>System.ServiceModel</AssemblyReference> 
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference> 
      <AssemblyReference>System.Xaml</AssemblyReference> 
      <AssemblyReference>System.Xml</AssemblyReference> 
      <AssemblyReference>System.Xml.Linq</AssemblyReference> 
      <AssemblyReference>WFTest.ActivityLibrary</AssemblyReference> 
      <AssemblyReference>mscorlib</AssemblyReference> 
      <AssemblyReference>WFTest.WorkflowConsole</AssemblyReference> 
    </sco:Collection> 
  </TextExpression.ReferencesForImplementation> 
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_1"> 
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1" Text="1111111111" /> 
    <local:noResultBookmark BookmarkName="xxxx" sap2010:WorkflowViewState.IdRef="noResultBookmark_1" /> 
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_2" Text="2222222" /> 
    <sads:DebugSymbol.Symbol>dzREOlxUZXN0XFdGVGVzdFxXRlRlc3QuV29ya2Zsb3dDb25zb2xlXEFjdGl2aXR5MS54YW1sBy0DMg4CAQEuBS5SAgEGLwUvaAIBBDAFME8CAQIuQy5PAgEHLyovMAIBBTBDMEwCAQM=</sads:DebugSymbol.Symbol> 
  </Sequence> 
  <sap2010:WorkflowViewState.ViewStateManager> 
    <sap2010:ViewStateManager> 
      <sap2010:ViewStateData Id="WriteLine_1" sap:VirtualizedContainerService.HintSize="211,62" /> 
      <sap2010:ViewStateData Id="noResultBookmark_1" sap:VirtualizedContainerService.HintSize="211,22" /> 
      <sap2010:ViewStateData Id="WriteLine_2" sap:VirtualizedContainerService.HintSize="211,62" /> 
      <sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="233,350"> 
        <sap:WorkflowViewStateService.ViewState> 
          <scg:Dictionary x:TypeArguments="x:String, x:Object"> 
            <x:Boolean x:Key="IsExpanded">True</x:Boolean> 
          </scg:Dictionary> 
        </sap:WorkflowViewStateService.ViewState> 
      </sap2010:ViewStateData> 
      <sap2010:ViewStateData Id="WFTest.WorkflowConsole.Activity1_1" sap:VirtualizedContainerService.HintSize="273,430" /> 
    </sap2010:ViewStateManager> 
  </sap2010:WorkflowViewState.ViewStateManager> 
</Activity>

活动 noResultBookmark

using System; 
using System.Activities; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
  
namespace WFTest.WorkflowConsole 
{ 
    public sealed class noResultBookmark : NativeActivity 
    { 
  
        public InArgument<string> BookmarkName { get; set; } 
  
        protected override bool CanInduceIdle 
        {  
            get 
            { return true; }  
        } 
  
  
        protected override void Execute(NativeActivityContext context) 
        { 
            string bookmark = context.GetValue(BookmarkName); 
  
            context.CreateBookmark(bookmark); 
  
            System.Console.WriteLine("创建bookmark:{0}", bookmark); 
        } 
    } 
}

 

控制类

 

using System; 
using System.Activities; 
using System.Activities.DurableInstancing; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.DurableInstancing; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
  
namespace WFTest.WorkflowConsole 
{ 
    public class MainClass 
    { 
  
  
        protected static AutoResetEvent reset = new AutoResetEvent(false); 
        public void Start() 
        { 
            WorkflowApplication application = new WorkflowApplication(new Activity1()); 
  
            SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore(Const.ConnectionString); 
            InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30)); 
            instanceStore.DefaultInstanceOwner = view.InstanceOwner; 
  
            application.InstanceStore = instanceStore; 
  
  
  
  
            application.Completed = new Action<WorkflowApplicationCompletedEventArgs>(a => 
            { 
                application = null; 
                System.Console.WriteLine("workflowCompleted:{0}", a.CompletionState.ToString()); 
            }); 
  
  
  
            application.OnUnhandledException = a => 
            { 
                System.Console.WriteLine("unhandledException:{0}", a.UnhandledException.Message); 
                return UnhandledExceptionAction.Cancel; 
            }; 
  
  
  
            application.Aborted = a => 
            { 
                application = null; 
                System.Console.WriteLine("aborted ,Reason:{0}", a.Reason.Message); 
            }; 
  
  
  
            application.Idle = a => 
            { 
                reset.Set(); 
                Console.WriteLine("OnIdle"); 
  
            }; 
               
  
            application.Run(); 
            reset.WaitOne(); 
            application.Unload(); 
  
            application.PersistableIdle = (e) => 
            { 
                return PersistableIdleAction.Unload; 
            }; 
  
  
  
            //获取工作流实列Id 
            Guid installGuid = application.Id; 
  
        } 
  
  
        public void Load() 
        { 
  
            string bookName = "xxxx"; 
  
  
  
  
            WorkflowApplication application = new WorkflowApplication(new Activity1()); 
  
            SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore(Const.ConnectionString); 
            InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30)); 
            instanceStore.DefaultInstanceOwner = view.InstanceOwner; 
  
            application.InstanceStore = instanceStore; 
  
  
            Guid guid = new Guid("0DAEC18F-D828-46B5-A397-D46A8A4DB4D1"); 
            application.Load(guid); 
  
  
  
            if (application.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1) 
            { 
  
                application.ResumeBookmark(bookName, bookName); 
  
            } 
        } 
    } 
}

入口类

using System; 
using System.Linq; 
using System.Activities; 
using System.Activities.Statements; 
using System.Diagnostics; 
using System.Collections.Generic; 
using System.Threading; 
using System.Activities.DurableInstancing; 
using System.Runtime.DurableInstancing; 
  
namespace WFTest.WorkflowConsole 
{ 
  
    class Program 
    { 
  
  
        static void Main(string[] args) 
        { 
  
  
            MainClass main = new MainClass(); 
            //      main.Start();  //创建流程 
            main.Load();     //加载流程 
  
  
            Console.WriteLine("End"); 
  
            Console.Read(); 
        } 
  
  
    } 
}

 

 

written by ocean