Search code examples
c#workflow-foundation-4

wf4 An activity can only access its own implementation variables


I have an activity with variables (which are C# expressions), but not able to read their values.

    public Collection<Variable> Variables { get; } = new Collection<Variable>();

    protected override void DoExecute(NativeActivityContext context)
    {
        var x = Variables.FirstOrDefault(...).Get(context);
    }

resulting in

Activity '1.1: MyActivity' cannot access this variable 
because it is declared at the scope of activity '1.1: MyActivity'.  
An activity can only access its own implementation variables.

I attempted to expose them via cachemetadata

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.SetImplementationVariablesCollection(Variables);
    }

And that results in

Exception <System.NotSupportedException: 
Expression Activity type 'CSharpValue`1' requires compilation in order to run.  
Please ensure that the workflow has been compiled.

My variables are c# expressions and compiled with

var wwfActivity = ActivityXamlServices.Load(xamlReader, new ActivityXamlServicesSettings {CompileExpressions = true});

enter image description here


Solution

  • I was able to hack around it with

    var var = context.DataContext.GetProperties()["variableName"];
    var value = var.GetValue(context.DataContext) as Whatever;
    

    without overriding the CacheMetadata method, but it feels a lil weird;