WF4 - 复合自定义活动在使用 OutArgument 时抛出一个奇怪的异常 [英] WF4 - Composite custom activity throw an strange exception when using OutArgument

查看:17
本文介绍了WF4 - 复合自定义活动在使用 OutArgument 时抛出一个奇怪的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用复合自定义活动,该活动应用正则表达式并在匹配时返回布尔值.模式是在设计时编码的东西.源文本来自一个活动.此活动也在设计时指定(我制作了一个活动设计器,允许将活动作为源删除)

I'm trying to use a composite custom activity that apply a regular expression and return a boolean if it match. The pattern is something encoded in design time. The source text is coming from an activity. This activity is also specified in design time (I've made a activity designer that allow to drop an activity as source)

但我还需要返回匹配表达式的子字符串,所以我添加了一个 OutArgument 来检索匹配的字符串,并捕获字符串.

But I also need to return what sub string match the expression, so I added an OutArgument to retrieve the string that match, and the string captured.

代码如下:

public class RegularExpression : NativeActivity<bool>
{
    [RequiredArgument]
    public string Pattern { get; set; }

    public OutArgument<string> Captured { get; set; }

    [RequiredArgument]
    public Activity<string> RetrieveTextActivity { get; set; }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.AddChild(this.RetrieveTextActivity);
    }

    protected override void Execute(NativeActivityContext context)
    {
        if (this.RetrieveTextActivity != null)
            context.ScheduleActivity<string>(this.RetrieveTextActivity, this.onRetrieveComplete);
    }

    private void onRetrieveComplete(NativeActivityContext context, ActivityInstance completedInstance, string result)
    {
        var regexp = new Regex(this.Pattern);
        var match = regexp.Match(result);

        this.Result.Set(context, match.Success);
        if (this.Captured != null)
            this.Captured.Set(context, match.Value);
    }
}

如果我执行此活动而没有将变量绑定到 Captured 参数,它会按预期工作(结果设置正确).
但是如果我使用设计器添加一个变量,那么我将变量绑定到 Captured 参数这个错误弹出:

If I execute this activity without binding a variable to the Captured argument, it works as expected (the Result is correctly set).
But if I use the designer to add a variable, then I bind the variable to the Captured argument this error popup:

不能使用类型为System.String"的参数.确保它是在活动中声明的.

The argument of type 'System.String' cannot be used. Make sure that it is declared on an activity.

执行这一行时抛出异常:

The exception is thrown when executing this line:

this.Captured.Set(context, match.Value);

有人知道为什么我不能设置参数吗?
我还读到我不应该测试 Captured 是否为空,运行时应该自动设置一个默认值.但是如果我不测试,当我没有将变量绑定到参数时,我会有一个 NullReference...

Does someone have an idea why I can't set the argument ?
I also read that I shouldn't test that Captured is null, the runtime should automatically set a default value. But If I don't test, I've a NullReference when I don't bind a variable to the argument...


我想添加有关工作流程本身的更多信息.我读过 another主题,它可能是 VS.在这里,我只想指定我正在使用重新托管的设计器来创建工作流(而不是 VS).然后,工作流会以 XML 格式保存在数据库中.
当我需要启动一个新的工作流时,我读取数据库,使用 XamlService.Load 并运行创建的工作流.


I want to add more information about the workflow itself. I've read in another topic that it may be VS. Here I just want to specify that I'm using a rehosted designer to create the workflow (and not VS). The workflow is then saved as XML in a database.
When I need to start a new workflow, I read the database, use XamlService.Load and Run the created workflow.

推荐答案

如果在 CacheMetadata 中声明参数,错误是否消失?

Does the error go away if you declare the argument in CacheMetadata?

protected override void CacheMetadata(NativeActivityMetadata metadata)
{
    metadata.AddChild(this.RetrieveTextActivity);

    RuntimeArgument argument = new RuntimeArgument("Captured", typeof(string), ArgumentDirection.Out);
    metadata.Bind(this.Captured, argument);
    metadata.AddArgument(argument);

}

我太快了.上面的代码现在应该可以编译并有望解决您的问题.

I was too quick. The above code should now compile and hopefully fix your problem.

这篇关于WF4 - 复合自定义活动在使用 OutArgument 时抛出一个奇怪的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆