0
votes

This is a know error when using C# expressions in windows workflow. The article at https://docs.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/csharp-expressions#CodeWorkflows explains the reason and how to fix it. It all works fine for me in standard workflows, but as soon as I add a custom NativeActivity to the WF, I get that same error again !

Below the code of how I load the XAML workflow and the simple NativeActivity (which is the ONLY activity in the test workflow and inside that activity is a simple assign expression).

Loading and invoking WF via XAML:

`XamlXmlReaderSettings settings = new XamlXmlReaderSettings()
{
    LocalAssembly = GetContextAssembly()
};
XamlReader reader = reader = ActivityXamlServices.CreateReader(new XamlXmlReader(fileURL, settings));
ActivityXamlServicesSettings serviceSettings = new ActivityXamlServicesSettings
{
    CompileExpressions = true
};
var activity = ActivityXamlServices.Load(reader, serviceSettings);
WorkflowInvoker.Invoke(activity);`

Doing it in code throws same Exception:

Variable<string> foo = new Variable<string>
            {
                Name = "Foo"
            };
            Activity activity = new Sequence
            {

                Variables = { foo },
                Activities =
                {
                    new TimeExecuteUntilAborted
                    {
                        Activities =
                        {
                            new Assign<string>
                            {
                                To = new CSharpReference<string>("Foo"),
                                Value = new CSharpValue<string>("new Random().Next(1, 101).ToString()")
                            }
                        }
                    }
                }
            };

            CompileExpressions(activity);//the method from the article mentioned above
            WorkflowInvoker.Invoke(activity);

The Native Activity:

[Designer("System.Activities.Core.Presentation.SequenceDesigner, System.Activities.Core.Presentation")]
public sealed class TimeExecuteUntilAborted : NativeActivity
{
    private Sequence innerSequence = new Sequence();

    [Browsable(false)]
    public Collection<Activity> Activities
    {
        get
        {
            return innerSequence.Activities;
        }
    }

    [Browsable(false)]
    public Collection<Variable> Variables
    {
        get
        {
            return innerSequence.Variables;
        }
    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.AddImplementationChild(innerSequence);
    }

    protected override void Execute(NativeActivityContext context)
    {
        context.ScheduleActivity(innerSequence);
    }
}
2

2 Answers

0
votes

Your TimeExecutedUntilAborted class seems to be the culprit. I was able to swap in one of my own template NativeActivities instead and your workflow executed fine with the expressions. I'm guessing that your class is causing an issue in the compiler method when it parses your code. I used this doc as an example for my NativeActivity: https://msdn.microsoft.com/en-us/library/system.activities.nativeactivity(v=vs.110).aspx.

0
votes

Sizzle Finger's answer is no solution but pointed me into the right direction to simply check what is different. It came out that the simple call to the base class method was missing:

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata); // !! This needs to be added
        metadata.AddImplementationChild(innerSequence);
    }