locked
Getting and setting the value of a variable inside of a code activity - with a twist! RRS feed

  • Question

  • I know that if I'm inside of a code activity, I can access a workflow variable with:

            protected override void Execute(CodeActivityContext context)
            {
                var myVar = context.DataContext.GetProperties().Find("var2", true);
                var myValue = myVar.GetValue(context.DataContext);
    
            }

    This works in the case where there is a sequence that has a variable declared and one of the activities needs to access it (I know...you should use an InArgument...but there's a long story behind the reason I need to do it this way...).

    However, this is my situation..

    I have a Declarative XAML-based activity called Activity1.  It contains a single activity called CodeActivity1.

    CodeActivity1 is trying to get a variable called "var1"

    "var1" is not declared on Activity1 (this is the reason its so crazy)

    Now...I Create a workflow...drop on a sequence and create "var1" on that sequence.

    then I drop on Activity 1, which contains CodeActivity1, which is trying to access the variable declared in the sequence.

    .....I know Confusing...

    It appears to be that when you get the DataContext, you're only getting variables that are declared within the closest ancestoral XAML file.

    In the scenario, I can't get to var1 from within CodeActivity 1....though you can is CodeActivity1 is sitting within the sequence that has the variable declared...

    So, can an activity that is nested relatively low in there access the variables?

    Workflow 
        Sequence with var1 declared
            Declarative Activity1
                CodeActivity1 trying to access var1

    Friday, February 10, 2012 5:08 PM

Answers

  • >It appears to be that when you get the DataContext, you're only getting variables that are declared within the closest ancestoral XAML file.

    This is correct. Notice that otherwise which variable you meant could become ambiguous, since two variables with the same name can exist, one per XAML file.

    If you need to smuggle data between layers of the workflow, you might be better off with something like scopes and execution properties, but it's hard to know given the lack of scenario details.
    Tim

    • Proposed as answer by Tim Lovell-Smith Wednesday, February 15, 2012 12:47 AM
    • Marked as answer by LeoTang Friday, February 17, 2012 12:23 PM
    Saturday, February 11, 2012 1:09 PM
  • Did you try using WorkflowInstanceExtension?  I haven't used this in exactly this context (used it from the host application) but you should be able to declare an extension and put your variable in there... then you can access it in your code activity using "Context.GetExtension".

    I asume your code activity knows the type of the variable...

    Hope that helps.


    Thanks, KBW

    • Marked as answer by LeoTang Friday, February 17, 2012 12:23 PM
    Sunday, February 12, 2012 7:07 AM

All replies

  • >It appears to be that when you get the DataContext, you're only getting variables that are declared within the closest ancestoral XAML file.

    This is correct. Notice that otherwise which variable you meant could become ambiguous, since two variables with the same name can exist, one per XAML file.

    If you need to smuggle data between layers of the workflow, you might be better off with something like scopes and execution properties, but it's hard to know given the lack of scenario details.
    Tim

    • Proposed as answer by Tim Lovell-Smith Wednesday, February 15, 2012 12:47 AM
    • Marked as answer by LeoTang Friday, February 17, 2012 12:23 PM
    Saturday, February 11, 2012 1:09 PM
  • Did you try using WorkflowInstanceExtension?  I haven't used this in exactly this context (used it from the host application) but you should be able to declare an extension and put your variable in there... then you can access it in your code activity using "Context.GetExtension".

    I asume your code activity knows the type of the variable...

    Hope that helps.


    Thanks, KBW

    • Marked as answer by LeoTang Friday, February 17, 2012 12:23 PM
    Sunday, February 12, 2012 7:07 AM