我该如何调用此工作流程?

时间:2015-01-15 21:54:35

标签: c# workflow-foundation-4 workflow-foundation

static void Main(string[] args)
{
      WorkflowDbContext db = new WorkflowDbContext();

      var workflowFromDb = db.Workflows.Find("1");

      string workFlowDefinition = workflowFromDb.WorkflowDefinition; 

      ActivityXamlServicesSettings settings = new ActivityXamlServicesSettings
      {
          CompileExpressions = true
      };
}

当我尝试这个时:

DynamicActivity<int> wf = ActivityXamlServices.Load(new StringReader(workFlowDefinition), settings) as DynamicActivity<int>;

我收到此错误:

CacheMetadata for activity 'PeakTriggerActivities.SalesToService' threw 'System.Xaml.XamlObjectWriterException: Cannot create unknown type '{clr-namespace:PeakTriggerActivities}GetTouchPoint'.

我也试过这个,但在这种情况下,wf只是变成了一个空值:

//Encoding this way since xml file shows utf-16
MemoryStream mStream = new MemoryStream(Encoding.Unicode.GetBytes(workFlowDefinition));
//Also tried LocalAssembly = System.Reflection.Assembly.GetExecutingAssembly() instead of LocalAssembly = typeof(GetTouchPoint).Assembly
DynamicActivity<int> wf = ActivityXamlServices.Load(new XamlXmlReader(mStream, new XamlXmlReaderSettings { LocalAssembly = typeof(GetTouchPoint).Assembly }), settings) as DynamicActivity<int>;

这是workFlowDefinition字符串包含的内容,名为GetTouchPoint的类与program.cs中的main方法位于同一个项目中:

<?xml version="1.0" encoding="utf-16"?>
<Activity mc:Ignorable="sap sap2010 sads" x:Class="PeakTriggerActivities.SalesToService"     sap2010:ExpressionActivityEditor.ExpressionActivityEditor="C#"     sap2010:WorkflowViewState.IdRef="PeakTriggerActivities.SalesToService_1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:local="clr-namespace:PeakTriggerActivities"
 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:t="clr-namespace:TouchPointModels;assembly=TouchPointModels"
 xmlns:t1="clr-namespace:TouchPointServices;assembly=TouchPointService"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <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>
      <x:String>TouchPointModels</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>Microsoft.CSharp</AssemblyReference>
      <AssemblyReference>PresentationCore</AssemblyReference>
      <AssemblyReference>PresentationFramework</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Activities</AssemblyReference>
      <AssemblyReference>System.Activities.Presentation</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.ServiceModel.Channels</AssemblyReference>
      <AssemblyReference>System.Xaml</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
      <AssemblyReference>System.Xml.Linq</AssemblyReference>
      <AssemblyReference>WindowsBase</AssemblyReference>
      <AssemblyReference>TouchPointModels</AssemblyReference>
      <AssemblyReference>TouchPointService</AssemblyReference>
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>PeakTriggerActivities</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_1">
    <Sequence.Variables>
      <Variable x:TypeArguments="x:String" Name="varTouchPointCode" />
      <Variable x:TypeArguments="t:TouchPoint" Name="varTouchPoint" />
      <Variable x:TypeArguments="x:Int32" Name="varProgramNumber" />
    </Sequence.Variables>
    <local:GetTouchPoint Text="{x:Null}" sap2010:WorkflowViewState.IdRef="GetTouchPoint_1"     InProgramNumber="1" InTouchPointCode="SATM">
      <local:GetTouchPoint.OutProgramNumber>
        <OutArgument x:TypeArguments="x:Int32">
          <mca:CSharpReference x:TypeArguments="x:Int32">varProgramNumber</mca:CSharpReference>
        </OutArgument>
      </local:GetTouchPoint.OutProgramNumber>
      <local:GetTouchPoint.OutTouchPoint>
        <OutArgument x:TypeArguments="t:TouchPoint">
          <mca:CSharpReference x:TypeArguments="t:TouchPoint">varTouchPoint</mca:CSharpReference>
        </OutArgument>
      </local:GetTouchPoint.OutTouchPoint>
      <local:GetTouchPoint.OutTouchPointCode>
        <OutArgument x:TypeArguments="x:String">
          <mca:CSharpReference x:TypeArguments="x:String">varTouchPointCode</mca:CSharpReference>
        </OutArgument>
      </local:GetTouchPoint.OutTouchPointCode>
    </local:GetTouchPoint>
    <If sap2010:WorkflowViewState.IdRef="If_1">
      <If.Condition>
        <InArgument x:TypeArguments="x:Boolean">
          <mca:CSharpValue x:TypeArguments="x:Boolean">varTouchPoint == null</mca:CSharpValue>
        </InArgument>
      </If.Condition>
      <If.Then>
        <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1">
          <InArgument x:TypeArguments="x:String">
            <mca:CSharpValue x:TypeArguments="x:String">"Cannot find Touchpoint Code :" +     varTouchPointCode</mca:CSharpValue>
          </InArgument>
        </WriteLine>
      </If.Then>
      <If.Else>
        <Sequence sap2010:WorkflowViewState.IdRef="Sequence_2">
          <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_3">
            <InArgument x:TypeArguments="x:String">
              <mca:CSharpValue x:TypeArguments="x:String">"Processing Touchpoint: " +  varTouchPoint.Code + " - " + varTouchPoint.Name</mca:CSharpValue>
            </InArgument>
          </WriteLine>
          <t1:ProcessTouchPoint InTouchPoint="{x:Null}" Success="{x:Null}" Text="{x:Null}" sap2010:WorkflowViewState.IdRef="ProcessTouchPoint_1">
            <t1:ProcessTouchPoint.InProgramNumber>
              <InArgument x:TypeArguments="x:Int32">
                <mca:CSharpValue x:TypeArguments="x:Int32">varProgramNumber</mca:CSharpValue>
              </InArgument>
            </t1:ProcessTouchPoint.InProgramNumber>
            <t1:ProcessTouchPoint.InTouchPointCode>
              <InArgument x:TypeArguments="x:String">
                <mca:CSharpValue x:TypeArguments="x:String">varTouchPointCode</mca:CSharpValue>
              </InArgument>
           </t1:ProcessTouchPoint.InTouchPointCode>
          </t1:ProcessTouchPoint>
        </Sequence>
      </If.Else>
    </If>
    <sads:DebugSymbol.Symbol>d0tDOlxwcm9qZWN0c1xQZWFrVHJpZ2dlckFjdGl2aXRpZXNcUGVha1RyaWdnZXJBY3Rpdml0aWVzXFNhbGVzVG9TZXJ2aWNlLnhhbWwRMANrDgIBATYFRhsCARtHBWkKAgECNoEBNocBAgEpOQs5YAIBJUMLQ2ICASE+Cz5iAgEdNmw2bwIBHEoLSl8CAQNOCVIVAgEWVQlnFAIBB1ANUH4CARdWC1oXAgERWwtmIgIBCFgPWJgBAgESYxFjYAIBDV4RXl4CAQk=    </sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="GetTouchPoint_1" sap:VirtualizedContainerService.HintSize="469,22" />
      <sap2010:ViewStateData Id="WriteLine_1" sap:VirtualizedContainerService.HintSize="211,62" />
      <sap2010:ViewStateData Id="WriteLine_3" sap:VirtualizedContainerService.HintSize="211,62" />
      <sap2010:ViewStateData Id="ProcessTouchPoint_1"   sap:VirtualizedContainerService.HintSize="211,22" />
      <sap2010:ViewStateData Id="Sequence_2" sap:VirtualizedContainerService.HintSize="233,248">
        <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="If_1" sap:VirtualizedContainerService.HintSize="469,398" />
      <sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="491,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="PeakTriggerActivities.SalesToService_1"   sap:VirtualizedContainerService.HintSize="531,664" />
</sap2010:ViewStateManager>
</sap2010:WorkflowViewState.ViewStateManager>
</Activity>

1 个答案:

答案 0 :(得分:1)

出于某种原因,这很有用。唯一的区别似乎是即使XamlXmlReader接受一个流作为它的第一个参数,你实际上也不能传入一个流,而是需要传入StringReader对象。

WorkflowDbContext db = new WorkflowDbContext();
var workflowFromDb = db.Workflows.Find("1");
ActivityXamlServicesSettings activitySettings = new ActivityXamlServicesSettings
{
    CompileExpressions = true
};
XamlXmlReaderSettings xamlReaderSettings = new XamlXmlReaderSettings { LocalAssembly = typeof(GetTouchPoint).Assembly };
XamlReader xamlReader = new XamlXmlReader(new StringReader(workflowFromDb.WorkflowDefinition), xamlReaderSettings);
var wf = ActivityXamlServices.Load(xamlReader, activitySettings);
var result = WorkflowInvoker.Invoke(wf);