Declare Variable<T> variable in a CodeActivity in windows workflow 4.0

时光总嘲笑我的痴心妄想 提交于 2019-12-08 03:58:31

问题


I am new to windows workflow and trying to get my head round variables, the following code gives me an error -

public sealed class CodeActivity1 : CodeActivity
{
    Variable<int> wfVar = new Variable<int>("wfVar", 0);

    protected override void Execute(CodeActivityContext context)
    {
        wfVar.Set(context, 1);
    }
}

Variable 'wfVar' of type 'System.Int32' cannot be used. Please make sure it is declared in an Activity or SymbolResolver.

What does the error mean, considering that I have declared the variable in an Activity.

Thanks, Ilias


回答1:


If you're trying to use a Variable as a CodeActivity implementation variable, you're thinking it wrong. CodeActivity should be used mostly to execute some code (within Execute() method of course), quickly, and exit.

What you want to do can be only achieved with NativeActivity. This way execution engine is aware of the existence of a variable that can be used (getted or setted) all over the activity execution.

public sealed class CustomActivity : NativeActivity
{
    Variable<int> wfVar = new Variable<int>("wfVar", 0);

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);

        metadata.AddImplementationVariable(wfVar);
    }

    protected override void Execute(CodeActivityContext context) 
    {
        wfVar.Set(context, 1);
    }
}



回答2:


The workflow runtime needs to be aware of your variables. By default it uses reflection over public properties. So making your variable public will do the trick. The other option is to use the CacheMetadata() function and register the variable there yourself.



来源:https://stackoverflow.com/questions/7767434/declare-variablet-variable-in-a-codeactivity-in-windows-workflow-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!