在NativeActivity中调度Receive活动

时间:2014-03-11 09:46:46

标签: c# workflow-foundation-4

在我的工作流服务中,我有一个包含大量方法的界面。所有这些都已使用“导入服务合同...”选项进入可用活动。

生成的活动很好,但我想更简单地使用它们。现在我需要记住设置关联处理程序,如果我忘记了一个,有时很难找到哪个Receive活动有问题。

所以我想围绕我的NativeActivity活动创建一个Receive包装器,其中InArgument<CorrelationHandle>设置[RequiredAttribute]标志,以使事情变得简单明了。< / p>

这是我在的地方:

public sealed class TestActivity : NativeActivity
{
    [RequiredArgument]
    public InArgument<CorrelationHandle> Handle { get; set; }
    [RequiredArgument]
    public InArgument<XName> ServiceContractName { get; set; }

    private Receive OnCGTaskCreate { get; set; }
    private Variable<CorrelationHandle> HandleVariable { get; set; }

    public TestActivity()
    {
        OnCGTaskCreate = new Receive()
        {
            OperationName = "OnCGTaskCreate",
            ServiceContractName = System.Xml.Linq.XName.Get("{Service.Workflow}IWorkflowService"),
            Action = "Service.Workflow/IWorkflowService/OnCGTaskCreate",
            DisplayName = "OnCGAccountCreate_Receive",
            CanCreateInstance = false,
            Content = new ReceiveParametersContent
            {
                Parameters =
                {
                    { "Context", new OutArgument<WorkflowExecutionContext>() },
                    { "Entity", new OutArgument<CGTask>() }
                }
            },
        };
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.AddImplementationChild(OnCGTaskCreate);
        HandleVariable = new Variable<CorrelationHandle>("correlationHandle");
        OnCGTaskCreate.CorrelatesWith = new InArgument<CorrelationHandle>(HandleVariable);
        metadata.AddImplementationVariable(HandleVariable);
        base.CacheMetadata(metadata);
    }

    protected override void Execute(NativeActivityContext context)
    {
        var handle = Handle.Get(context);
        HandleVariable.Set(context, handle);
        OnCGTaskCreate.CorrelatesOn = new System.ServiceModel.MessageQuerySet() { { "key1", new XPathMessageQuery("sm:body()/xgSc:OnCGTaskCreate/xgSc:Context/xg0:WorkflowId") } };
        OnCGTaskCreate.ServiceContractName = ServiceContractName.Get(context);

        context.ScheduleActivity(OnCGTaskCreate, new CompletionCallback(OnCallCompleted));
    }

    private void OnCallCompleted(NativeActivityContext context, ActivityInstance instance)
    {
      //figure this part out later
    }
}

问题是我一直得到以下异常:

The operation cannot be performed on variables of Handle subtypes.

   at System.Activities.Variable`1.VariableLocation.set_Value(T value)
   at System.Activities.ActivityContext.SetValueCore[T](LocationReference locationReference, T value)
   at Service.Workflow.Activity.TestActivity.Execute(NativeActivityContext context) in Service.Workflow\Activity\TestActivity.cs:line 53
   at System.Activities.NativeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

此外,由于我只使用过设计器中的Receive活动,因此我不确定我是否在代码中正确实现了它。

以下是Receive活动的示例XAML代码以及它在设计器中的使用方式:

                        <p:Receive Action="Service.Workflow/IWorkflowService/OnCGTaskCreate" DisplayName="OnCGTaskCreate_Receive" sap2010:WorkflowViewState.IdRef="Receive_1" OperationName="OnCGTaskCreate" ProtectionLevel="None" ServiceContractName="p1:IWorkflowService">
                          <p:Receive.CorrelatesOn>
                            <p:XPathMessageQuery x:Key="key1">
                              <p:XPathMessageQuery.Namespaces>
                                <ssx:XPathMessageContextMarkup>
                                  <x:String x:Key="xg0">http://schemas.datacontract.org/2004/07/Service.Base</x:String>
                                  <x:String x:Key="xgSc">Service.Workflow</x:String>
                                </ssx:XPathMessageContextMarkup>
                              </p:XPathMessageQuery.Namespaces>sm:body()/xgSc:OnCGTaskCreate/xgSc:Context/xg0:WorkflowId</p:XPathMessageQuery>
                          </p:Receive.CorrelatesOn>
                          <p:Receive.CorrelatesWith>
                            <InArgument x:TypeArguments="p:CorrelationHandle">
                              <mca:CSharpValue x:TypeArguments="p:CorrelationHandle">handle</mca:CSharpValue>
                            </InArgument>
                          </p:Receive.CorrelatesWith>
                          <p:ReceiveParametersContent>
                            <OutArgument x:TypeArguments="acgsb:WorkflowExecutionContext" x:Key="Context" />
                            <OutArgument x:TypeArguments="acgsbc:CGTask" x:Key="Entity">
                              <mca:CSharpReference x:TypeArguments="acgsbc:CGTask">task</mca:CSharpReference>
                            </OutArgument>
                          </p:ReceiveParametersContent>
                        </p:Receive>

因此,总结一下 - 如何通过在NativeActivity内以编程方式安排运行上述XAML代码来简化它的使用?

编辑:

我使用Variable的原因是因为this博客文章描述了如何安排内部活动。

可悲的是,这篇文章是关于安排Receive活动之外的其他内容,所以很可能它不适用于我的问题。

0 个答案:

没有答案