如何从本机活动中的计划活动中获取OutArgument?

时间:2011-03-03 19:42:42

标签: c# .net workflow-foundation-4 workflow-activity

我有一个由Receive活动和SendReply活动组成的活动。接收活动等待消息,然后将其写入控制台,然后发送回复。

问题是,当完成时,我无法从Receive活动中获取消息。

下面是我的活动类和异常信息。

public sealed class MyActivity : NativeActivity
{
    private Receive Receive { get; set; }
    private SendReply SendReply { get; set; }

    private Variable<CorrelationHandle> CorrelationHandle { get; set; }
    private Variable<string> MessageParameter { get; set; }
    private Variable<string> ResponseParameter { get; set; }

    public MyActivity()
    {   
        CorrelationHandle = new Variable<CorrelationHandle>("SenseProcessCorrelationHandle");
        MessageParameter = new Variable<string>("MessageParamter");
        ResponseParameter = new Variable<string>("ResponseParam");

        Receive = new Receive
        {
            ServiceContractName = XName.Get("{http://tempuri.org/}MyService" ),
            OperationName = "Trigger",
            Content = new ReceiveParametersContent
            {
                Parameters =
                {
                    { "Message", new OutArgument<string>(MessageParameter) },
                },
            },
            CanCreateInstance = true, 
            CorrelationInitializers =
            {
                new RequestReplyCorrelationInitializer
                {
                    CorrelationHandle = new InArgument<CorrelationHandle>(CorrelationHandle),
                }
            }
        };

        SendReply = new SendReply
        {
            Request = Receive,
            Content = new SendMessageContent
            {
                Message = new InArgument<string>(ResponseParameter),
            }
        };

    }

    protected override void Execute(NativeActivityContext context)
    {
        context.ScheduleActivity(Receive, OnReceiveCompleted);
    }

    void OnReceiveCompleted(NativeActivityContext context, ActivityInstance inst)
    {

        /* Next code line throws exception
         * 
         * Activity '2: MyActivity' cannot access this variable because it is 
         * declared at the scope of activity '2: MyActivity'.  An activity 
         * can only access its own implementation variables.
         */
        Console.WriteLine(MessageParameter.Get(context));             

        context.ScheduleActivity(SendReply);
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.AddChild(Receive);
        metadata.AddChild(SendReply);
        metadata.AddVariable(CorrelationHandle);
        metadata.AddVariable(ResponseParameter);
        metadata.AddVariable(MessageParameter);            
    }
}

如果有人知道我做错了什么,我将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

您必须输入此代码而不是代码:

CorrelationInitializers = 
{                 
    new RequestReplyCorrelationInitializer  
    {
        CorrelationHandle = CorrelationHandle 
    }          
}

第二个CorrelationHandle是你的变量,不会引起新的争论。