加载XAML的COMPILED工作流中的NativeActivity抛出表达式活动类型'CSharpValue1'需要编译才能运行

时间:2017-06-15 16:05:04

标签: workflow-foundation-4 workflow-foundation

在Windows工作流程中使用C#表达式时,这是一个已知错误。 https://docs.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/csharp-expressions#CodeWorkflows上的文章解释了原因以及解决方法。在标准工作流程中,这一切都适合我,但是只要我向WF添加自定义NativeActivity,我就会再次遇到同样的错误!

下面是我如何加载XAML工作流和简单NativeActivity的代码(这是测试工作流中唯一的活动,在该活动中是一个简单的赋值表达式)。

通过XAML加载和调用WF:

`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);`

在代码中执行此操作会抛出相同的异常:

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

原生活动:

[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 个答案:

答案 0 :(得分:0)

你的TimeExecutedUntilAborted课程似乎是罪魁祸首。我能够交换我自己的一个模板NativeActivities,并且您的工作流程可以很好地处理表达式。我猜测你的类在解析代码时会在编译器方法中引起问题。我使用此文档作为NativeActivity的示例:https://msdn.microsoft.com/en-us/library/system.activities.nativeactivity(v=vs.110).aspx

答案 1 :(得分:0)

Sizzle Finger的答案不是解决方案,但我指出了正确的方向,只需检查有什么不同。结果是缺少对基类方法的简单调用:

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