将NativeActivity变量公开给工作流设计器

时间:2010-06-09 22:00:48

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

我有一个NativeActivity,其中包含一个Activity正文。此活动的目的是将子活动持续时间内的资源公开为Variable。我遇到的问题是Variable似乎无法在活动之外使用。我将使用StreamReader作为示例资源。

ResourceActivity.cs:

[Designer(typeof(ResourceActivityDesigner))]
public sealed class ResourceActivity : NativeActivity
{
    [RequiredArgument]
    public InArgument<string> Path { get; set; }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public Activity Body { get; set; }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public Variable<StreamReader> Resource { get; set; }

    public ResourceActivity()
    {
        this.Resource = new Variable<StreamReader>
        {
            Default = null,
            Name = "reader"
        };
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        if (this.Path != null) metadata.AddArgument(this.Path);
        if (this.Body != null) metadata.AddChild(this.Body);
        if (this.Resource != null) metadata.AddVariable(this.Resource);
    }

    protected override void Execute(NativeActivityContext context)
    {
        this.Resource.Set(context, new StreamReader(this.Path.Get(context)));
        context.ScheduleActivity(this.Body, new completionCallback(Done), new FaultCallback(Faulted));
    }

    private void Done(NativeActivityContext context, ActivityInstance instance)
    {
        var reader = this.Reader.Get(context);
        if (reader != null) reader.Dispose();
    }

    private void Faulted(NativeActivityFaultContext context, Exception ex, ActivityInstance instance)
    {
        var reader = this.Reader.Get(context);
        if (reader != null) reader.Dispose();
    }
}

我无法在工作流设计器的“变量”列表中查看“资源”或“阅读器”。我错过了CacheMetadata中的内容吗?

2 个答案:

答案 0 :(得分:0)

如果要在活动本身和子项之间共享变量,则需要将Body定义为ActivityAction类型而不是Activity。请参阅Matt Winkler撰写的this博文。

答案 1 :(得分:0)

似乎答案是在课堂上添加Collection<Variable>CacheMetadata是不必要的。

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Collection<Variable> Variables { get; private set; }

public ResourceActivity()
{
    this.Resource = new Variable<StreamReader> { Default = null, Name = "reader" };
    this.Variables = new Collection<Variable>(new [] { this.Resource });
}

编辑:不,现在发生错误,表明变量已定义多次。如果我省去了将资源添加到变量集合中,它就不会显示...

编辑#2:我将此提交为Microsoft Connect上的错误,并被告知这是一个已知错误,but it is not targetted for a fix in VS 2010. It has been postponed.