如何在 .NET 的工作流 Activity 中使用外部 class?

How to use external class in Workflow Activity in .NET?

我在 class 库中定义了一个 class:

namespace TestLib
{
    public class TestClass
    {
        public static void Test() { }
    }
}

我还在 XAML 文件中定义了一个 Activity。现在我想在 activity 中使用 InvokeMethod activity 来调用 class 中的静态方法,但调试器会抛出运行时异常:

"Could not load file or assembly 'TestLib, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."

class 库被正确引用,我也可以在 XAML 源代码中看到它。 如果我将此 class 包含到源代码中,它可以正常工作。

在工作流中使用外部类型的正确方法是什么?我做错了什么?谢谢。

更新:

重现这个案例非常简单。

创建一个名为TestLibClass Library项目并添加上面的TestClass.

创建一个 Activity Library 项目并将 main activity 重命名为 TestActivity。添加对 TestLib 的引用。添加一个Sequence activity,里面有一个InvokeMethod activity,用来调用方法TestClass.Test。这是 XAML 代码:

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WorkflowSimpleTest.TestActivity" sap2010:ExpressionActivityEditor.ExpressionActivityEditor="C#" sap2010:WorkflowViewState.IdRef="WorkflowSimpleTest.Activity1_1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 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:t="clr-namespace:TestLib;assembly=TestLib"
 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>
    </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>TestLib</AssemblyReference>
      <AssemblyReference>WorkflowSimpleTest</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_1">
    <InvokeMethod sap2010:WorkflowViewState.IdRef="InvokeMethod_1" MethodName="Test" TargetType="t:TestClass" />
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1" Text="End activity." />
    <sads:DebugSymbol.Symbol>d1dEOlxFbHZlZGluXFByb2plY3RzXEF1dG9tYXRpb24mQklcc291cmNlXFNFQ1VSSVRBU1xXb3JrZm9sd1NpbXBsZVRlc3RcVGVzdEFjdGl2aXR5LnhhbWwEJgMqDgIBAScFJ3ECAQQoBShVAgECKEMoUgIBAw==</sads:DebugSymbol.Symbol>
  </Sequence>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="InvokeMethod_1" sap:VirtualizedContainerService.HintSize="218,130" />
      <sap2010:ViewStateData Id="WriteLine_1" sap:VirtualizedContainerService.HintSize="218,62" />
      <sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="240,356">
        <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="WorkfolwSimpleTest.Activity1_1" sap:VirtualizedContainerService.HintSize="280,436" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>

创建控制台应用程序并使用以下代码执行工作流:

Dictionary<string, object> vars = new Dictionary<string, object>() { };
WorkflowApplication wfApp = new WorkflowApplication(new TestActivity(), vars);
wfApp.Completed += delegate (WorkflowApplicationCompletedEventArgs e)
{
    Console.WriteLine("Completed.");
};
wfApp.Idle += delegate (WorkflowApplicationIdleEventArgs e1)
{
    Console.WriteLine("Idle.");
};
wfApp.Run();

应用程序应该抛出运行时异常。

错误如其所说。显然,通过在 Workflow Library 项目中添加对库的引用,我也应该在宿主项目中添加引用。