views:

34

answers:

2

I've got a NativeActivity which contains an Activity body. The purpose of this activity is to expose a resource for the duration of the child activity as a Variable. The problem I've encountered is it appears the Variable cannot be used outside the activity. I'll use StreamReader as an example resource.

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();
    }
}

I cannot view "Resource" or "reader" in the Variables list in the Workflow Designer. Am I missing something in CacheMetadata?

A: 

You need to define your Body as type ActivityAction instead of Activity if you want to share a variable between the activity itself and the children. See this blog post by Matt Winkler.

Maurice
A: 

It appears the answer was adding an Collection<Variable> to the class. CacheMetadata was unnecessary.

[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 });
}

EDIT: Nope, now an error occurs which states the Variable is defined multiple times. If I leave out adding the resource to the variables collection it just doesn't show up...

EDIT #2: I submitted this as a bug on Microsoft Connect and was told this is a known bug, but it is not targetted for a fix in VS 2010. It has been postponed.

sixlettervariables