在设计时在复合WF4活动中设置内部属性

时间:2010-12-01 23:47:30

标签: wcf workflow-foundation workflow-foundation-4

我想创建一个包含预定义ReceiveAndSendReply活动的复合Windows工作流活动(在.NET 4下)。某些属性是预定义的,但其他属性(特别是ServiceContractName)需要在设计器中设置。

我可以将其作为活动模板实现(与实现ReceiveAndSendReply的方式相同),但不愿意。如果我稍后更改模板,则必须手动更新以前创建的所有工作流程。模板还允许其他开发人员更改应修复的属性。

有没有办法从Xaml活动中执行此操作?我还没有找到一种方法来为嵌入式Activity的属性赋予Argument值。如果没有,你会建议采用什么技术?

1 个答案:

答案 0 :(得分:6)

我没有使用复合XAML活动完成此操作,并且在尝试时遇到一些错误但是通过NativeActivity这样做是没有问题的。请参阅下面的示例代码。

public class MyReceiveAndSendReply : NativeActivity
{
    private Receive _receive;
    private SendReply _sendReply;

    public string ServiceContractName { get; set; }
    public string OperationName { get; set; }

    protected override bool CanInduceIdle
    {
        get { return true; }
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        _receive = _receive ?? new Receive();
        _sendReply = _sendReply ?? new SendReply();
        _receive.CanCreateInstance = true;
        metadata.AddImplementationChild(_receive);
        metadata.AddImplementationChild(_sendReply);

        _receive.ServiceContractName = ServiceContractName;
        _receive.OperationName = OperationName;

        var args = new ReceiveParametersContent();
        args.Parameters["firstName"] = new OutArgument<string>();
        _receive.Content = args;

        _sendReply.Request = _receive;

        var results = new SendParametersContent();
        results.Parameters["greeting"] = new InArgument<string>("Hello there");
        _sendReply.Content = results;

        base.CacheMetadata(metadata);
    }

    protected override void Execute(NativeActivityContext context)
    {
        context.ScheduleActivity(_receive, ReceiveCompleted);

    }

    private void ReceiveCompleted(NativeActivityContext context, ActivityInstance completedInstance)
    {
        context.ScheduleActivity(_sendReply);
    }
}